我的循环永远不会停止,我似乎不明白出了什么问题。我正在做 我的类(class)的一个项目,我是循环新手,所以有点困惑。请告诉我怎么做 来解决这个问题。
import java.util.Scanner;
public class FracCalc {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); {
boolean Quit = true;
System.out.println("Welcome to FracCalc");
System.out.println("Type expressions with fractions, and I will evaluate them");
String answer = scan.nextLine();
while (Quit = true) {
if (answer.equals("Quit")){
System.out.println("Thanks forr running FracCalc!");
break;
} else {
System.out.println("I can only process 'Quit' for now");
}
}
}
}
}
最佳答案
Quit = true
会将 true
赋给 Quit
,并返回 true
。因此,您正在执行 while (true)
,这是一个规范的无限循环。即使您正在测试 Quit == true
(注意双等号),您也永远不会将其分配给 false
,正如 Izcd 所说。您可以使用 if
来break
,但是 answer
仅在循环之外分配一次。
关于java - 为什么我的 "While"循环一直持续下去?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599772/