做了个简单小例子测试异常,非常吊诡的是如果输入非整形,就会陷入死循环,一直车轱辘提示无效要求重输,但又不给我机会重输,只是自己一个劲儿车轱辘。我明明在do循环里有输入动作啊,为啥只执行第一次,后面就一直跳过直接到catch呢?
package prj7;
import java.util.Scanner;
public class ExcepTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
boolean inputFlag = false;
int id;
System.out.println("Please input a number:");
do{
try{
id = input.nextInt();
inputFlag = true;
}catch (Exception e){
System.out.println("The input must be an integer,please try again");
}
}while (inputFlag==false);
System.out.println("your input is correct!");
}
}