代码需求很简单,输入一个数字并打印出来,如果输入的不是数字,那么就一直循环。实现的方法有很多,偶然间发现这段代码会出现让我难以理解的bug,希望大佬们指点指点
代码如下:
static void read1(){
Scanner scan =new Scanner(System.in);
while(true){
int n;
System.out.print("enter a number:");
try {
n =scan.nextInt();
}catch(InputMismatchException e){
continue;
}
System.out.println(n);
break;
}
}