爲什麼如果有InputMismatchException,這段代碼會繼續循環? 當你輸入一個零時,catch
可以工作,但是如果你輸入一個字符串,循環就會變成無限的。如果存在InputMismatchException,爲什麼此代碼會繼續循環?
import java.util.*;
public class TryCatch
{
public static void main(String args[])
{
int age, age2, sum;
boolean repeat=true;
Scanner input = new Scanner(System.in);
do
{
try
{
System.out.println("Enter age: ");
age = input.nextInt();
System.out.print("Enter age2: ");
age2 = input.nextInt();
sum = age/age2;
System.out.print(sum);
repeat=false;
}
catch (Exception e)
{
System.out.println("Your error is "+e+"\n Try again");
}
}
while(repeat == true);
}
}
來吧格式雅編寫雅野蠻人 –