gpt4 book ai didi

java - 当从用户那里读取其中一个时,为什么不是 "Ellie"== "Ellie"?

转载 作者:行者123 更新时间:2023-12-03 20:24:19 24 4
gpt4 key购买 nike

<分区>

以下问题来 self 几周前参加的测验,但答错了,但没有提供答案:

Consider the following code, which contains no compile errors:

String secret = "Ellie";
Scanner kb = new Scanner(System.in);
System.out.println("Guess which name I'm thinking of:");
String guess = kb.next();

if (guess == secret)
{
System.out.println("Wow! You're smart!");
}
else
{
System.out.println("Wrong!");
System.out.println("You guessed: " + guess);
System.out.println("The correct answer was: " + secret);
}

Suppose the user enters "Ellie" at the prompt. What output would result, and why that output and not the other output?

这是我的错误答案:

The output would be the else statement "WRONG!" because of the capitalization of the letter 'E'. A solution to this would be to either change the String secret to "ellie" as well as the user's guess, or one can write in the ignoreCase keyword to String secret.

程序居然输出“Wrong”,我测试了一下。

除了简单地知道答案之外,有人真的可以向我解释一下,以便我更好地理解这个概念吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com