如果带有字符串比较的语句永远不会运行?

发布于 2024-11-30 17:37:28 字数 638 浏览 1 评论 0原文

import java.util.Scanner;

class Test6
{
    public static void main (String[] args)
    {
        int z = 0;
        while (z != 1)
        {
            System.out.print("Please enter your name: ");
            Scanner x = new Scanner (System.in);
            String name = x.nextLine();
            System.out.print("\n"+"So your name is "+name+"?: ");
            Scanner y = new Scanner (System.in);
            String answer = y.nextLine();
            if ((answer == "yes")||(answer == "Yes"));
            {
                z = 1;
            }
        }
        System.out.println("\n"+"Great!");
    }
}
import java.util.Scanner;

class Test6
{
    public static void main (String[] args)
    {
        int z = 0;
        while (z != 1)
        {
            System.out.print("Please enter your name: ");
            Scanner x = new Scanner (System.in);
            String name = x.nextLine();
            System.out.print("\n"+"So your name is "+name+"?: ");
            Scanner y = new Scanner (System.in);
            String answer = y.nextLine();
            if ((answer == "yes")||(answer == "Yes"));
            {
                z = 1;
            }
        }
        System.out.println("\n"+"Great!");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

南城旧梦 2024-12-07 17:37:28

使用"yes".equalsIgnoreCase(answer)

Use "yes".equalsIgnoreCase(answer).

时光磨忆 2024-12-07 17:37:28

使用 <代码>String#equals(),而不是==。已经有很多关于此的问题了。

if ("yes".equals(answer) || "Yes".equals(answer));
{
    z = 1;
}

在您的情况下, String#equalsIgnoreCase() 可能更合适:

if ("yes".equalsIgnoreCase(answer));
{
    z = 1;
}

旁注:您将 z 用作布尔标志。不使用int,而是使用boolean

boolean z = true;
while (z)
{

}

Use String#equals(), not ==. There are many questions on SO about this already.

if ("yes".equals(answer) || "Yes".equals(answer));
{
    z = 1;
}

In your case, String#equalsIgnoreCase() might more suitable:

if ("yes".equalsIgnoreCase(answer));
{
    z = 1;
}

Side note: you're using z like a boolean flag. Instead of using an int, use a boolean.

boolean z = true;
while (z)
{

}
萌面超妹 2024-12-07 17:37:28

使用 equals 或在您的情况下可能是 equalsIgnoreCase

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

 }

Use equals or in your case may be equalsIgnoreCase

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

 }
朱染 2024-12-07 17:37:28

您想查看 String.equals()String.equalsIgnoreCase(),例如 answer.equalsIgnoreCase("yes") 或甚至“yes”.equalsIgnoreCase(answer)

You want to have a look at String.equals() or String.equalsIgnoreCase(), e.g. answer.equalsIgnoreCase("yes") or even "yes".equalsIgnoreCase(answer).

昔日梦未散 2024-12-07 17:37:28

在 String 对象上调用 equals("str") 将会起作用。

if(answer.equals("yes") || answer.equals("no"))
{
   z = 1;
}

== 将引用与字符串进行比较,因为 Java 中的 String 是一个对象。

Calling equals("str") on the String object will work.

if(answer.equals("yes") || answer.equals("no"))
{
   z = 1;
}

== compares references with strings, as String in Java is an object.

森林散布 2024-12-07 17:37:28

您混淆了对象标识和对象相等的概念。

在 Java 编程语言中,等于运算符 (==) 测试左侧和右侧操作数是否引用同一个确切对象(也称为引用相等或对象标识);但是, Object#equals(o) 方法(由 String 类重写)测试调用对象是否与其参数“等效”。

考虑这些例子:

String s = "foo";
s == s; // => true, because the variable "s" obviously refers
// to the same object as itself.  They are "identical".

另一方面:

String a = new String("foo");
String b = new String("foo");
a == b; // => false, because there are two separate objects.
// However...
a.equals(b); // => true, because the strings have the same
// character sequences.  They are "equal".

You are confusing the concepts of object identity and object equality.

In the Java programming language, the equals operator (==) tests if the left-hand and right-hand operands refer to the same exact object (aka referential equality, or object identity); however, the Object#equals(o) method (which is overridden by the String class) tests if the calling object is "equivalent" to its argument.

Consider these examples:

String s = "foo";
s == s; // => true, because the variable "s" obviously refers
// to the same object as itself.  They are "identical".

On the other hand:

String a = new String("foo");
String b = new String("foo");
a == b; // => false, because there are two separate objects.
// However...
a.equals(b); // => true, because the strings have the same
// character sequences.  They are "equal".
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文