為什么在catch里加上input = new Scanner(System.in);能夠阻止無限循環(huán)
public static int inputCommand()
{
int a;
try
{
a = input.nextInt();
return a;
}catch(Exception e)
{
input = new Scanner(System.in);//阻止無限循環(huán)
//System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
return -1;
}
}
2019-08-06
拋出異常,然后處理異常,處理完之后進(jìn)行下一步