如果在 while 循环内,则无法跳出 while 循环

If inside a while loop, can't break out of the while loop

我最近开始研究 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 时,循环中断并转到字符串完毕。如果你能用简单的代码帮助我解决这个问题,我将不胜感激,因为我不知道任何更高级的东西。

您可以标记循环,然后在 break 语句中使用该标签指定要跳出的循环,例如

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

从描述上看不太清楚,但是continue会让你跳到循环的结尾,继续循环的其余部分,你可以使用标志来控制你是否想退出不止一层。

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

中断 Java 中的标签是另一种选择,但它会使代码变得脆弱:您受到一些顽固的重构者的摆布,他们可能会倾向于移动 "break to label" 幸福地没有意识到后果;编译器无法警告此类恶作剧。

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

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);
}

希望对您有所帮助。