參考答案里inputcommand為啥要加上console= new Scanner(System.in),不加輸入字符就死循環了
private static int inputCommand(){
int command;
try {
command = console.nextInt();
return command;
} catch (Exception e) {
//若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出”錯誤命令異常“
console = new Scanner(System.in);
//返回-1
return -1;
}
}
2019-01-06
查了好久,這個console = new Scanner(System.in);的意思是之前的掃描儀有個緩存,你輸入了字符,拋出異常之后再重新輸入,但是這個緩存內的數據沒有清空,不需要你再輸入,里面一直有個異常,所以一直報錯,他現在新建了個新的掃描儀,把之前數據清空了
2018-08-15
你這個程序需要輸入數據,你沒有
console= new Scanner(System.in),
雜么輸入進去,你都沒數據,他不就是死循環