java java输入字符串并在输入“停止”时停止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4284078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:41:17  来源:igfitidea点击:

java input string and stop when 'stop' is entered

javaloops

提问by Tom

this is my code and I have a do-while loop which should carry on unless the string "text" entered is "stop". However when I compile the code it doesnt stop and stuck in an infinite loop. Pease help. Thanks.

这是我的代码,我有一个 do-while 循环,除非输入的字符串“text”是“stop”,否则它应该继续执行。但是,当我编译代码时,它不会停止并陷入无限循环。皮斯帮助。谢谢。

import java.io.*;

public class input
{
    public static void main(String[] argv) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        String text = "";  
        do
        {
            System.out.println("Please enter a string: ");
            text = br.readLine(); 
            System.out.println(text);
        }
        while (text != "stop");
    }
}

回答by Nicolas Repiquet

Try replacing text != "stop"by !text.equals("stop")

尝试替换text != "stop"!text.equals("stop")

!=is a reference equality test, .equals()is a logical equality test. Two strings can be different objects and still be logicaly equals (same content in this case).

!=是参考相等性测试,.equals()是逻辑相等性测试。两个字符串可以是不同的对象,但在逻辑上仍然是相等的(在这种情况下是相同的内容)。

回答by Jesper

You are comparing strings with !=. That does not work in Java. You should use equals()to compare strings:

您正在将字符串与!=. 这在 Java 中不起作用。您应该使用equals()来比较字符串:

while (!text.equals("stop"));

Note that ==and !=on objects compare the references- i.e., if you use those operators on non-primitive variables, you are checking if the variables refer to the same object, and not if the content of those objects is the same.

请注意,==and !=on objects 比较引用- 即,如果您在非原始变量上使用这些运算符,则您正在检查变量是否引用同一个对象,而不是这些对象的内容是否相同。

回答by Boris Pavlovi?

Replace

代替

while (text != "stop")

with

while (!text.equals("stop"))

回答by khachik

You cannot compare strings using ==. Use equals: while(!"stop".equals(text)).

您不能使用==. 使用equalswhile(!"stop".equals(text))

Also since it comes from the user input, you might want to compare ignoring the case, you can use equalsIgnoreCase.

此外,由于它来自用户输入,您可能想要比较忽略大小写,您可以使用equalsIgnoreCase.

回答by PeterMmm

All posters are right: you should use String.equals().

所有海报都是对的:您应该使用 String.equals()。

Also you should complete understand String handling in Java. You will need it everywhere.

您还应该完全了解 Java 中的字符串处理。你会在任何地方都需要它。

Even that String.equals should be used Java offers some "magic", that works even with your comparison. Keep learning !

即使应该使用 String.equals,Java 也提供了一些“魔法”,即使进行比较也能起作用。保持学习 !

public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String text = "";
        "stop".intern();
        do {
            System.out.println("Please enter a string: ");
            text = br.readLine().intern();
            System.out.println(text);
        } while (text != "stop");
    }