读取并检查用户输入的字符串
我有这样的代码:
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String answer = input.nextLine();
if(answer == "yes"){
System.out.println("Yea I programmed this right!");
}else{
System.out.println("Awww :(");
}
}
}
但是当我运行它并输入yes时,它应该说
“是的,我编程正确!”
但它说
“Awww :( ”
I have this code:
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String answer = input.nextLine();
if(answer == "yes"){
System.out.println("Yea I programmed this right!");
}else{
System.out.println("Awww :(");
}
}
}
But when I run it and type yes, it should be saying
"Yea I programmed this right!"
but it says
"Awww :("
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您比较字符串的方式不正确。您必须使用
equals()
方法,如下所示:当您使用 Java 编程时,运算符
==
通常用于比较基本数据类型(int
、double
等)。如果您使用==
来比较两个对象类型(如字符串),您就是在比较它们的身份,即检查它们是否引用内存中的同一对象。在您的情况下,您需要的是比较它们是否相等:即使它们是两个不同的对象,它们是否具有完全相同的值(在本例中为字符串) - 并且对于您必须使用equals()
方法。编辑:
更好的是,为了防止出现
NullPointerException
,翻转比较顺序并首先写入要比较的字符串被认为是一个很好的做法,如下所示:解释很简单:如果由于某种原因
answer
为null
,则上述比较将评估为false
(意思是:answer
不是"yes"
),而当尝试在上调用
值。equals()
方法时,代码的第一个版本会导致NullPointerException
>nullYou're comparing strings incorrectly. You must use the
equals()
method, like this:When you're programming in Java, the operator
==
is generally used for comparing primitive data types (int
,double
, etc.). If you use==
for comparing two object types (like strings), you're comparing them for identity, that is, checking if they reference the same object in memory. In your case, what you need is to compare if they're equal: if they have the exact same value (a string of characters in this case) even if they're two different objects - and for that you must use theequals()
method.EDIT :
Even better, for preventing a
NullPointerException
, it's considered a good practice flipping the order of the comparison and writing first the string you're comparing with, like this:The explanation is simple: if for some reason
answer
isnull
, the above comparison will evaluate tofalse
(meaning:answer
is not"yes"
), whereas the first version of the code would cause aNullPointerException
when trying to call theequals()
method on anull
value.应该是
(对于
String
相等而言,==
是不正确的,我们处理answer
为 null 的情况)should be
(
==
is not correct forString
equality, and we handle the case whereanswer
is null)使用
String.equals()
而不是==
。在 Java 中,
==
正在测试这 2 个字符串是否是完全相同的实例,其中“a”!=“a”。相反,您需要测试"a".equals("a")
。因此替换
为:
请注意,这里翻转顺序是有意的,因为如果
answer
为 null,这可以防止 NullPointerException - 因为"yes".equals(null)
将简单地返回false,而不是抛出异常。 (对null
调用操作会抛出 NullPointerException,即null.equals("yes")
。)Use
String.equals()
instead of==
.In Java,
==
is testing that the 2 Strings are the exact same instance, where "a" != "a". Instead, you need to test for"a".equals("a")
.So replace
with:
Note that flipping the order here is intentional, as this can prevent a NullPointerException if
answer
was null - as"yes".equals(null)
will simply return false, instead of throwing an exception. (Calling an operation onnull
would throw a NullPointerException, I.E.null.equals("yes")
.)更改此
equals()
方法将这个 字符串(示例中的答案)与指定对象进行比较。当且仅当参数不为 null 并且是表示与此对象相同的字符序列的 String 对象时,结果为 true。重要的是要了解
equals()
方法和==
运算符执行两种不同的操作。正如刚才提到的,equals()
方法比较 String 对象内的字符。==
运算符比较两个对象引用以查看它们是否引用同一个实例。Change this
The
equals()
method compares this string (answer in your example) to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.It is important to understand that the
equals()
method and the==
operator perform two different operations. As just mentioned, theequals()
method compares the characters inside a String object. The==
operator compares two object references to see whether they refer to the same instance.导入java.util.Scanner;
公共类示例{
公共静态无效主(字符串[]参数){
}
}
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
}
}
或者您可以尝试使用“compareTo()”函数
or you can try to use "compareTo()" function