我的循环永不停止,我似乎无法理解错误。我正在做 我的班级的项目,我是新的循环,所以有点混乱。请告诉我怎么样 解决这个问题。
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");
}
}
}
}
}
答案 0 :(得分:6)
Quit = true
会将true
分配给Quit
,然后返回true
。因此,你正在做while (true)
,一个规范的无限循环。即使您正在测试Quit == true
(注意双重等号),也不要像Izcd评论那样将其分配给false
。您可以使用break
if
,但answer
仅在循环外分配一次。
答案 1 :(得分:1)
将String answer = scan.nextLine();
放入循环中。
尝试以下方法:
import java.util.Scanner;
public class FracCalc {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to FracCalc");
System.out.println("Type expressions with fractions, and I will evaluate them");
String answer = scan.nextLine();
do {
if (answer.equals("Quit")) {
System.out.println("Thanks forr running FracCalc!");
break;
} else {
System.out.println("I can only process 'Quit' for now");
}
answer = scan.nextLine();
} while (true);
}
}