我遇到了一个奇怪的问题.
我试图让这个代码循环不断,直到用户输入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.
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
你应该使用:
while (Quit_Detect)
代替:
while (Quit_Detect = true)
第一个语句检查if是否Quit_Detect为true,第二个语句将值设置Quit_Detect为true.
| 归档时间: |
|
| 查看次数: |
37 次 |
| 最近记录: |