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