[英]While loop not cancelling out
我遇到了一個奇怪的問題。
我試圖使此代碼不斷循環,直到用戶輸入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.
}
}
}
}
您應該使用:
while (Quit_Detect)
代替:
while (Quit_Detect = true)
第一條語句檢查Quit_Detect
是否為true,第二條語句將Quit_Detect
的值Quit_Detect
為true。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.