我有一个作业要实现一个简单的测试应用程序,以下是我的当前代码:
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循环
呢?
break scanner
(停止scanner)。 - noamtm