Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么这个基本的主方法while循环不起作用?没有语法错误_Java_While Loop - Fatal编程技术网

Java 为什么这个基本的主方法while循环不起作用?没有语法错误

Java 为什么这个基本的主方法while循环不起作用?没有语法错误,java,while-loop,Java,While Loop,-我删掉了主代码,因为它似乎不必要,问题似乎在于此,代码运行一次,然后即使我键入“是”作为扫描仪的答案,循环也会停止。它只循环一次 String bool = "yes"; while (bool == "yes") { int e1 = whichExam(); printinfo(pp, csn, isa, fwt, lds, afl, oop, e1); Scanner scanner = new Scanner(System.in)

-我删掉了主代码,因为它似乎不必要,问题似乎在于此,代码运行一次,然后即使我键入“是”作为扫描仪的答案,循环也会停止。它只循环一次

String bool = "yes";
    while (bool == "yes") {
        int e1 = whichExam();
        printinfo(pp, csn, isa, fwt, lds, afl, oop, e1);
        Scanner scanner = new Scanner(System.in);
        System.out.println("Do you want to check another exam ('yes' or 'no')?");
        bool = scanner.nextLine();
    }
简短答复:

您正在错误地比较字符串:

while (bool == "yes")
应该是:

while (bool.equals("yes"))
详细回答:


当您在bool==yes时执行此操作时,您正在检查两个字符串引用是否指向内存中的同一对象,因为在第一次检查条件后情况并非如此,因此循环只执行一次。你可能也想用。

啊,行得通,我会接受答案,因为它让我很难重复,我不知道问题出在哪里,如果我知道的话,我一开始就不用问了。如果你判断像这样的重复,一个原始的问题几乎是不可能的。