我遇到了一个奇怪的问题。
我试图让这段代码不断循环,直到用户输入 4;当用户输入 4 时,我想将“Quit_Detect”设置为 false。
出于某种原因,它不允许我这样做。代码仍然不断循环,直到它被手动停止。
下面是我用于该程序的所有代码以及一些注释。
import java.util.Scanner; // Imports the scanner utility.
public class Start {
public static void main(String[] args) {
Scanner Reader = new Scanner(System.in); // Creates a new scanner.
@SuppressWarnings("unused")
boolean Quit_Detect;
Quit_Detect = true;
while (Quit_Detect = true)
{
int input; // States that input will have a datatype of 'int', or integer. Or, a whole number.
System.out.println("Please input your option.");
System.out.println("1. Door with a crack in it");
System.out.println("2. Normal Wooden Door");
System.out.println("3. Turn around");
System.out.println("4. Quit");
input = Reader.nextInt(); // Has the user define what the variable 'input' will be set to.
switch (input) // Uses the Variable 'input' to detect what case to follow.
{
case 1:System.out.println("First Option");
break;
case 2:System.out.println("Second Option");
break;
case 3:System.out.println("Third Option");
break;
case 4:Quit_Detect = false;
break;
default:System.out.println("Invalid option."); //Prints this if the user inputs any number other than 1, 2, or 3.
}
}
}
}
最佳答案
你应该使用:
同时 (Quit_Detect)
代替:
同时 (Quit_Detect = true)
第一个语句检查 Quit_Detect
是否为真,第二个语句将 Quit_Detect
的值设置为真。
关于java - while 循环不抵消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33651734/