【发布时间】: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呢?
【问题讨论】:
标签: java while-loop break