读取并检查用户输入的字符串

发布于 2024-12-23 08:42:45 字数 485 浏览 4 评论 0原文

我有这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

吃兔兔 2024-12-30 08:42:45

您比较字符串的方式不正确。您必须使用 equals() 方法,如下所示:

if (answer.equals("yes"))

当您使用 Java 编程时,运算符 == 通常用于比较基本数据类型(intdouble 等)。如果您使用 == 来比较两个对象类型(如字符串),您就是在比较它们的身份,即检查它们是否引用内存中的同一对象。在您的情况下,您需要的是比较它们是否相等:即使它们是两个不同的对象,它们是否具有完全相同的值(在本例中为字符串) - 并且对于您必须使用 equals() 方法。

编辑:

更好的是,为了防止出现NullPointerException,翻转比较顺序并首先写入要比较的字符串被认为是一个很好的做法,如下所示

if ("yes".equals(answer))

:解释很简单:如果由于某种原因 answernull,则上述比较将评估为 false(意思是:answer 不是"yes"),而当尝试在 上调用 equals() 方法时,代码的第一个版本会导致 NullPointerException >null 值。

You're comparing strings incorrectly. You must use the equals() method, like this:

if (answer.equals("yes"))

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 the equals() 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:

if ("yes".equals(answer))

The explanation is simple: if for some reason answer is null, the above comparison will evaluate to false (meaning: answer is not "yes"), whereas the first version of the code would cause a NullPointerException when trying to call the equals() method on a null value.

七婞 2024-12-30 08:42:45
if(answer == "yes"){

应该是

if("yes".equals(answer)){

(对于 String 相等而言,== 是不正确的,我们处理 answer 为 null 的情况)

if(answer == "yes"){

should be

if("yes".equals(answer)){

(== is not correct for String equality, and we handle the case where answer is null)

烟火散人牵绊 2024-12-30 08:42:45

使用 String.equals() 而不是 ==

在 Java 中,== 正在测试这 2 个字符串是否是完全相同的实例,其中“a”!=“a”。相反,您需要测试 "a".equals("a")

因此替换

if(answer == "yes"){

为:

if("yes".equals(answer)){

请注意,这里翻转顺序是有意的,因为如果 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

if(answer == "yes"){

with:

if("yes".equals(answer)){

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 on null would throw a NullPointerException, I.E. null.equals("yes").)

也只是曾经 2024-12-30 08:42:45

更改此

if(answer.equals("yes")){
    System.out.println("Yea I programmed this right!");
}else{
    System.out.println("Awww :(");
}

equals() 方法将这个 字符串(示例中的答案)与指定对象进行比较。当且仅当参数不为 null 并且是表示与此对象相同的字符序列的 String 对象时,结果为 true。

重要的是要了解 equals() 方法和 == 运算符执行两种不同的操作。正如刚才提到的,equals() 方法比较 String 对象内的字符。 == 运算符比较两个对象引用以查看它们是否引用同一个实例。

Change this

if(answer.equals("yes")){
    System.out.println("Yea I programmed this right!");
}else{
    System.out.println("Awww :(");
}

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, the equals() method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance.

爱你不解释 2024-12-30 08:42:45

导入java.util.Scanner;

公共类示例{
公共静态无效主(字符串[]参数){

Scanner input = new Scanner(System.in);
String answer = input.nextLine();

/*Edit your next line as mine,u'll get the correct ans...*/

if("yes".equals(answer)){ 
    System.out.println("Yea I programmed this right!");
}else{
    System.out.println("Awww :(");
}

}
}

import java.util.Scanner;

public class Example {
public static void main(String[] args) {

Scanner input = new Scanner(System.in);
String answer = input.nextLine();

/*Edit your next line as mine,u'll get the correct ans...*/

if("yes".equals(answer)){ 
    System.out.println("Yea I programmed this right!");
}else{
    System.out.println("Awww :(");
}

}
}

饭团 2024-12-30 08:42:45

或者您可以尝试使用“compareTo()”函数

private static Scanner input;
private static String choice;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    input = new Scanner(System.in);
    choice = input.nextLine();

    if (choice.compareTo("yes") == 0) {
        System.out.println("Yea I programmed this right!");
    } else {
        System.out.println("Awww :(");
    }

}

or you can try to use "compareTo()" function

private static Scanner input;
private static String choice;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    input = new Scanner(System.in);
    choice = input.nextLine();

    if (choice.compareTo("yes") == 0) {
        System.out.println("Yea I programmed this right!");
    } else {
        System.out.println("Awww :(");
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文