【问题标题】:If inside a while loop, can't break out of the while loop如果在 while 循环内,则无法跳出 while 循环
【发布时间】:2015-12-28 10:38:43
【问题描述】:

我最近开始学习 Java,在测试一些东西时遇到了一个问题。这可能是一个非常简单的问题,但我似乎无法解决它。 这是我的代码:

    int firstj = 1;

    if (firstj == 1) {
        String choice = "Type a number between 1 and 4";
        System.out.println(choice);
        while (true) {

            if (firstj == 1) {
                Scanner third = new Scanner(System.in);
                String thirdch = third.nextLine();

                while (true) {

                    if (thirdch.equals("1")) {
                        System.out.println("Show choice and accept input again ");
                        System.out.println(choice);
                        break;
                    } else if (thirdch.equals("2")) {
                        System.out.println("Show choice and accept input again ");
                        System.out.println(choice);
                        break;
                    } else if (thirdch.equals("3")) {
                        System.out.println("Show choice and accept input again ");
                        System.out.println(choice);
                        break;
                    }

                    else if (thirdch.equals("4")) {
                        // I need this to break the loop and move on to the
                        // "Done." string
                        break;
                    }

                    else {
                        System.out.println("Type a number between 1 and 4");
                        thirdch = third.nextLine();
                    }
                }

            }
        }
    }
    String done = "Done";
    System.out.println(done);

我想这样当你输入 1、2 或 3 时,你会得到一个字符串,告诉你再次输入一个数字并接受用户输入,而当你输入 4 时,循环中断并转到字符串完成。如果你能用简单的代码帮助我解决这个问题,我将不胜感激,因为我不知道任何更高级的东西。

【问题讨论】:

  • 你的意思是要跳出循环吗?
  • 您正在突破内部 while 循环,而不是外部 while 循环。所以 while(true) 总是正确的。
  • "break" 只能让您脱离 1 个循环。您最基本的问题是无限循环。有一个无限循环已经够糟糕了,你实际上有两个。是时候重构你的代码了。

标签: java loops if-statement while-loop break


【解决方案1】:

你可以像这样使用锁来停止:

public static void main(String[] args) throws Exception {
    int firstj = 1;
    if (firstj == 1) {
        boolean lock = true;
        while (lock) {

            if (firstj == 1) {
                Scanner third = new Scanner(System.in);

                while (lock) {

                    System.out.println("Type a number between 1 and 4");
                    String thirdch = third.nextLine();
                    switch (thirdch) {
                    case "1":
                        System.out
                                .println("Show choice and accept input again ");
                        break;
                    case "2":
                        System.out
                                .println("Show choice and accept input again ");
                        break;
                    case "3":
                        System.out
                                .println("Show choice and accept input again ");
                        break;
                    case "4":
                        lock = false;
                        break;
                    }

                }
                third.close();

            }
        }
    }
    String done = "Done";
    System.out.println(done);
}

希望对你有帮助。

【讨论】:

    【解决方案2】:

    打破嵌套循环的最具扩展性的方法是将整个内容放在一个函数中并使用return

    在 Java 中中断标签是另一种选择,但它会使代码变得脆弱:您受制于一些顽固的重构者,他们可能会乐于移动“中断标签”而没有意识到后果;编译器无法警告此类恶作剧。

    【讨论】:

    • 也许你可以举一些例子来说明需要注意的脆性?这种“未指明的坏处”在咬你之前很难被发现。
    • 我认为 Bathsheba 只是试图指出当添加更多循环并且代码变得更大更复杂时,这种 break 语句的用法可能很难遵循,我完全同意他的观点。
    • @ChristopherYang 我并不反对:我实际上不记得我最后一次使用标签 break 是什么时候了。我认为通常有更简单的方法来构建代码以避免它。我的观点是,对于不熟悉它所产生的各种问题的人来说,描述代码易碎是什么意思会很有用。
    【解决方案3】:

    描述不是很清楚,但是continue 会让你跳到循环的末尾,继续循环的其余部分,你可以使用标志来控制是否要退出不止一级。

    【讨论】:

      【解决方案4】:

      你可以给循环加标签,然后在你的break语句中使用标签来指定你想跳出哪个循环,例如

      outer: while (true) {
        while (true) {
          break outer;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-01-03
        • 2013-05-06
        • 1970-01-01
        • 2020-04-17
        • 2021-08-29
        • 1970-01-01
        • 2015-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多