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

3

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

你的意思是你要跳出外部循环吗? - Andy Turner
你正在跳出内部while循环,但没有跳出外部while循环。因此,while(true)始终为真。 - Casey ScriptFu Pharr
“break” 只能让你跳出一个循环。你最根本的问题是无限循环。拥有一个无限循环已经够糟糕了,而你实际上有两个。现在是时候重构你的代码了。 - Christopher Yang
4个回答

7

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

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

5
最可扩展的退出嵌套循环的方法是将整个循环放入函数中并使用return。在Java中,通过标签跳出循环是另一个选择,但它可能会使代码变得脆弱:你会听从某些固执的重构者的任性之言,他们可能会移动“跳转到标签”,而毫不知情其后果;编译器无法警告此类恶作剧。

1
也许你可以给出一些要注意的脆弱性示例?这种“未指明的瑕疵”很难察觉,直到它咬了你。 - Andy Turner
我认为Bathsheba只是试图指出,当添加更多循环并且代码变得更大更复杂时,使用break语句可能会很难跟踪,我完全同意他的观点。 - Christopher Yang
@ChristopherYang 我并不反对:实际上我无法记得我上一次使用带有标签的 break 是什么时候了;我认为通常有更简单的代码结构可以避免使用它。我的观点是,对于不熟悉它产生的问题类型的人来说,描述代码是脆弱的意义是有用的。 - Andy Turner

2
您可以使用锁来停止,就像这样:
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);
}

我希望这能有所帮助。

1
从描述中并不十分清楚,但是continue将让您跳到循环的结尾,以继续执行循环的其余部分,并且您可以使用标志来控制是否要退出多个级别。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接