1 回答

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊
int a = -1;
while(true)
{
try
{
Scanner in = new Scanner(System.in);
a = in.nextInt();
}
catch(InputMismatchException e)
{
e.printStackTrace();
}
//Your switch statement
}
順便說一下,a 的默認(rèn)值設(shè)置為 -1。如果發(fā)生異常,則進(jìn)入 switch case 時(shí)的值將是 -1。
int a = -1;
while(true)
{
try
{
Scanner in = new Scanner(System.in);
a = in.nextInt();
}
catch(InputMismatchException e)
{
e.printStackTrace();
continue;
}
//Your switch statement
}
如果您使用 continue 則循環(huán)的當(dāng)前迭代將停止并運(yùn)行下一次迭代
添加回答
舉報(bào)