Java 如何在 switch 语句下中断 while 循环?

78

我有一个作业要实现一个简单的测试应用程序,以下是我的当前代码:

import java.util.*;

public class Test{

private static int typing;

public static void main(String argv[]){
    Scanner sc = new Scanner(System.in);
    System.out.println("Testing starts");
    while(sc.hasNextInt()){
        typing = sc.nextInt();
        switch(typing){
            case 0:
              break; //Here I want to break the while loop
            case 1:
              System.out.println("You choosed 1");
              break;
            case 2:
              System.out.println("You choosed 2");
              break;
            default:
              System.out.println("No such choice");
        }
    }
      System.out.println("Test is done");
    }
}

我现在想做的是,当用户按下0时,表示用户想要退出测试,那么我要中断while循环并打印测试已完成,但它并不像这样工作,我知道原因可能是"break"中断了switch,那么我该如何让它中断while循环呢?

5个回答

183

你可以给 while 循环加上标签,然后 break 这个被标记的循环,具体操作如下:

loop: while(sc.hasNextInt()){
    typing = sc.nextInt();
    switch(typing){
        case 0:
          break loop; 
        case 1:
          System.out.println("You choosed 1");
          break;
        case 2:
          System.out.println("You choosed 2");
          break;
        default:
          System.out.println("No such choice");
    }
}

而且 label 可以是你想要的任何单词,例如 "loop1"


41
“loop”这个标签选择很差/无聊。更有趣的选择包括“up”、“out”、“stuff”、“theBank”和“dance”。 - jakber
6
开玩笑的话先放在一边,给循环加上一个更具信息性的标签(在你的情况下可能是“scanner”),这样break语句就可以写成break scanner(停止scanner)。 - noamtm
@noamtm 那不是很明显,所以感谢你指出来。 - John R Perry

14
你需要一个布尔变量,例如shouldBreak
    boolean shouldBreak = false;
    switch(typing){
        case 0:
          shouldBreak = true;
          break; //Here I want to break the while loop
        case 1:
          System.out.println("You choosed 1");
          break;
        case 2:
          System.out.println("You choosed 2");
          break;
        default:
          System.out.println("No such choice");
    }
    if (shouldBreak) break;

5
将while循环放入一个函数中,当你按下0时,不要使用break,而是使用return。例如:
    import java.util.*;

public class Test{

private static int typing;

public static void main(String argv[]){
    Scanner sc = new Scanner(System.in);
    func(sc);
      System.out.println("Test is done");
    }
}

public static void func(Scanner sc) {


    System.out.println("Testing starts");
    while(sc.hasNextInt()){
        typing = sc.nextInt();
        switch(typing){
            case 0:
              return; //Here I want to break the while loop
            case 1:
              System.out.println("You choosed 1");
              break;
            case 2:
              System.out.println("You choosed 2");
              break;
            default:
              System.out.println("No such choice");
        }
    }
}

}

1
如何终止内部菜单? 示例代码:
import java.util.Scanner;

public class Example {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); //used to get input
        int option1, option2 = 0;
        boolean loop_terminate = true; //flag used to terminate inner while loop

        //Main Menu
        while (true) {
            //Main Menu options
            System.out.println("1.Option 1");
            System.out.println("2.Option 2");
            System.out.println("3.Option 3");
            System.out.println("4.Option 4");
            System.out.println("5.Exit main menu");

            System.out.print("Please enter your choice : ");
            option1 = input.nextInt();

            switch (option1) {

                case 1:
                       //do something here    
                    break;
                case 2:
                       //do something here 
                    break;
                case 3:

                    while (loop_terminate) {
                        //Inner menu options
                        System.out.println("1.Inner Menu option 1");
                        System.out.println("2.Inner Menu option 2");
                        System.out.println("3.Inner Menu option 3");
                        System.out.println("4.Return to Main Menu");

                        System.out.print("Please enter your choice : ");
                        option2 = input.nextInt();
                        switch (option2) {

                            case 1:
                                break;
                            case 2:
                                break;
                            case 3:
                                break;
                            case 4:
                                loop_terminate = false; //this will terminate inner menu
                                break;
                            default:
                                System.out.println("Invalid option");
                                break;
                        }
                    }
                    break; //never forget to add this break statement
                case 4:
                      break;
                case 5:
                    return; //terminate outer menu

                default:
                    System.out.println("Invalid option");
            }
        }

    } 
}

0
这里有一个相对简单的方法来做到这一点!
由于您希望在将值分配给typing为0时中断while循环,最简单的方法是在进入switch-case语句之前检查typing的值。
import java.util.*;

public class Test{
    private static int typing;

    public static void main(String argv[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Testing starts");
    
        while(sc.hasNextInt()){
            typing = sc.nextInt();
        
            if (typing == 0){
                break;
            } 
        
            else{
                switch(typing){
                    case 1:
                        System.out.println("You chose 1");
                        break;
                    case 2:
                        System.out.println("You chose 2");
                        break;
                    default:
                        System.out.println("No such choice");
                }
            }
        }
        System.out.println("Test is done");
    }
}

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