首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java如何在switch语句下中断switch循环?

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

Stack Overflow用户
提问于 2014-04-02 21:24:01
回答 4查看 62.9K关注 0票数 73

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

代码语言:javascript
复制
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呢?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-04-02 21:26:06

您可以label自己的while循环,并break labeled loop,它应该如下所示:

代码语言:javascript
复制
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"

票数 167
EN

Stack Overflow用户

发布于 2014-04-02 21:25:13

您需要一个布尔变量,例如shouldBreak

代码语言:javascript
复制
    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;
票数 14
EN

Stack Overflow用户

发布于 2014-04-02 21:40:06

将时间放在函数中,当您按0而不是换行时,只需return。例如:

代码语言:javascript
复制
    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");
        }
    }
}

}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22823395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档