Java如何在switch语句下打破while循环?

Posted

技术标签:

【中文标题】Java如何在switch语句下打破while循环?【英文标题】:Java How can I break a while loop under a switch statement? 【发布时间】:2014-05-14 10:35:22 【问题描述】:

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

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 loop并打印Test is done,但它不会那样工作,我知道原因可能是"break"打破了switch,我怎么能让它打破while loop呢?

【问题讨论】:

【参考方案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");
            
        

     

【讨论】:

【参考方案2】:

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



【讨论】:

【参考方案3】:

你可以label你的while循环,breaklabeled loop,应该是这样的:

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"

【讨论】:

"loop" 是一个糟糕/无聊的标签选择。更有趣的选择包括“up”、“out”、“stuff”、“theBank”和“dance”。 玩笑不谈,给循环一个信息量更大的标签(在你的情况下,可能是scanner),让中断看起来像break scanner(停止扫描)。 @noamtm 这并不明显,所以感谢您指出这一点。【参考方案4】:

您需要一个布尔变量,例如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;

【讨论】:

以上是关于Java如何在switch语句下打破while循环?的主要内容,如果未能解决你的问题,请参考以下文章

c++中for循环和switch语句哪个更高效

为啥这个 switch 语句在运行时会结束 while 循环?

Java基础第4天+switch语句for,while,do...while循环语句break,return,continue控制跳转语句

打破一个while循环并重新启动代码

java基础二~三(运算符条件语句switch循环语句while)

JAVA 循环结构 - for, while 及 do...while