????????????????Scanner?input=new?Scanner(system.in)
????????????????
???? //輸入類型不匹配進入死循環
while(true)?{
try?{
System.out.println("請輸入第"+i+"個玩家的ID:");
p.ID=input.nextInt();
break;
}catch?(Exception?e)?{
System.out.println("請輸入整數!");
continue;
}
}
//正常代碼
while(true)?{
System.out.println("請輸入第"+i+"個玩家的ID:");
String?str?=?input.next();
try?{
p.ID=Integer.parseInt(str);
break;
}catch?(Exception?e)?{
System.out.println("請輸入整數!");
continue;
}
}
關于輸入類型不匹配的異常處理,為什么我第一種代碼會進入死循環?是input.next()的值沒有變嗎?
_Devil___4213279
2017-11-08 19:51:40