简体   繁体   中英

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

I recently started studying Java and came across a problem while testing out something. This might be a very easy question, but I can't seem to solve it. This is my code:

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

I want to make it so that when you type 1, 2 or 3, you get the string telling you to type a number again and accept user input, while when you type 4 the loop breaks and goes to the String done. I would be grateful if you could help me solve this problem with an easy code, since I don't know any of the more advanced things.

You can label loops, and then use the label in your break statement to specify which loop you want to break out of, eg

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

The most scalable way of breaking out of a nested loop is to put the whole thing in a function and use return .

Break to a label in Java is the other option, but it can make code brittle: you are at the mercy of some recalcitrant refactorer who might feel inclined to move the "break to label" blissfully unaware of the consequences; a compiler is unable to warn of such shenanigans.

You could use a lock to stop, like that:

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

I hope it helps.

从描述中不太清楚,但是continue将让你跳到循环结束,继续循环的其余部分,你可以使用标志来控制你是否要退出多个级别。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM