java - while 循环不抵消

标签 java eclipse

我遇到了一个奇怪的问题。

我试图让这段代码不断循环,直到用户输入 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/

相关文章:

eclipse - Mac 上的 STS 启动问题

java - 找不到标记库描述符

java - 为什么没有将操作结果放在并发 HashMap 上? putIfAbsent 的用例是什么?

java - Ubuntu:如何让 apt-get/synaptic 为自己的应用程序工作

java - Android - 如何在 View 组内设置 subview

java - Eclipse 到 IntelliJ IDEA : Create 'Run As' Configurations

android - 生命周期配置错误未涵盖的插件执行

java - 键盘后面的选项卡 - Android

java - 没有用于 EntityManager 的持久性提供程序,JPA 配置

java - LibGDX - tile/tilemaps 中的纹理文本