0 java loops if-statement while-loop
我的循环永远不会停止,我似乎无法理解什么是错的.我正在为我的班级做一个项目,我对循环很新,所以有点混乱.请告诉我如何解决此问题.
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");
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Quit = true将分配true到Quit,并返回true.因此,你正在做while (true)一个规范的无限循环.即使你正在测试Quit == true(注意双重等号),你也不会false像Izcd评论那样分配它.您可以在循环外break使用if,但answer只能分配一次.