【问题标题】:While loop terminates without changing the condition's valueWhile 循环终止而不改变条件的值
【发布时间】:2020-07-18 16:28:52
【问题描述】:

我正在使用此代码:

    public void menu() {
        boolean finish = false;
        while (!finish) {
            // print menu
            int n = // acquire selection through other method of the program
            switch (n) {
                case 0:
                    finish = true;
                    break;
                case 1:
                    // do stuff 1
                    break;
                case 2:
                    // do stuff 2
                    break;
                case 3:
                    // do stuff 3
                    break;
                case 4:
                    // do stuff 4
                    break;
            }
        }

    }

我尝试将finish = true; 放在所有break; 之前,但while 循环不断终止。 finish 变量不在其他任何地方使用。

我需要在 n == 0 之前不要终止

【问题讨论】:

  • > 我试着把finish = true;就在所有休息时间之前;
  • 当我运行你的代码时(给n赋值),循环无限运行(不能证明,但它一直运行到我中断程序)。无法复制

标签: java loops while-loop


【解决方案1】:

我假设你只想在 n = 0 时终止

  public static void main(String[] args) {

    boolean finish = false;
    while (!finish) {
        // print menu
        System.out.println("NEXT!");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        switch (n) {
            case 0:
                finish = true;
                break;
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            case 3:
                System.out.println("3");
                break;
            case 4:
                System.out.println("4");
                break;
        }
    }
}

上面的代码有效

【讨论】:

    【解决方案2】:
    public class Test {
        public static void main(String[] args) {
            WhileTest whileTest = new WhileTest();
            whileTest.menu();
            System.out.println("Just finished menu function");
        }
    }
    
    class WhileTest {
        public void menu() {
            boolean finish = false;
            while (!finish) {
                // print menu
                int n = 0; // acquire selection through other method of the program
                switch (n) {
                    case 0:
                        finish = true;
                        System.out.println(0);
                        break;
                    case 1:
                        // do stuff 1
                        System.out.println(1);
                        break;
                    case 2:
                        // do stuff 2
                        System.out.println(2);
                        break;
                    case 3:
                        // do stuff 3
                        System.out.println(3);
                        break;
                    case 4:
                        // do stuff 4
                        System.out.println(4);
                        break;
                }
                System.out.println("After the switch block");
            }
    
        }
    
    
    }
    

    请注意,您没有在 switch 语句中给出任何默认条件。而且,当您不在 switch 块中给出默认语句时,代码将完全有效。但是,程序的执行是在 switch 块之后开始的。

    记下以下场景:

    场景1:当你给n = 0时,输出将如下:

    0
    After the switch block
    Just finished menu function
    

    它第一次进入 switch 块并将完成变量设置为 true。所以执行只会发生一次。

    场景 2: 当您给出 n = 1 或 2 或 3 或 4 时。输出将如下:

    4
    After the switch block
    4
    After the switch block
    4
    After the switch block
    

    上面两行的无限流。完成变量永远不会为真。您必须手动停止该程序。

    场景 3: 当您给出 n = 5 或 case 块中不存在的任何内容时。输出如下:

    After the switch block
    After the switch block
    After the switch block
    After the switch block
    After the switch block
    After the switch block
    

    这将是上述行的无限流。因为它不会进入任何 case 块,也不会在任何地方修改变量 finish。

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 2010-09-13
      • 1970-01-01
      相关资源
      最近更新 更多