【发布时间】:2018-02-03 23:40:16
【问题描述】:
我正在尝试在 JAVA 中的 while 循环中使用 try-catch 语句,只是为了在将字符串输入提供给 nextInt 时捕获异常,我不知道为什么它在第一次错误输入后继续引发异常。
代码
import java.util.Arrays;
import java.util.Scanner;
public class People{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Scanner inS=new Scanner(System.in);
int i=0;
System.out.println("Enter number of people");
int n=in.nextInt();
int[] age=new int[n];
String[] name=new String[n];
double[] aIncome=new double[n];
while (i<n){
try{
System.out.println(i+"Enter your last and first name (for e.g. if full name is \"Rahul Gupta\" then enter \"Gupta Rahul\") : ");
name[i]=inS.nextLine();
System.out.println("Now putting name");
System.out.println("Enter your age: ");
age[i]=in.nextInt();
System.out.println("Now putting age");
System.out.println("Enter your annual income: ");
aIncome[i]=in.nextDouble();
System.out.println("Now putting income");
i++;
}
catch(Exception InputMismatchException){
System.out.println("Error !! Wrong input, Please try again.");
}
}
System.out.println(Arrays.toString(age));
System.out.println(Arrays.toString(name));
System.out.println(Arrays.toString(aIncome));
}
}
当输入正确时。
Script started on Sat 03 Feb 2018 07:55:42 PM NST
amehla@slbnen3000pc50:~/Desktop/test$ jva KKKava People.K
Enter number of people
3
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") :
harit Gupta
Now putting name
Enter your age:
26
Now putting age
Enter your annual income:
59495
Now putting income
1Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") :
Rahul Gupta
Now putting name
Enter your age:
59
Now putting age
Enter your annual income:
35695
Now putting income
2Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") :
Shikha kom
Now putting name
Enter your age:
56
Now putting age
Enter your annual income:
59565
Now putting income
[26, 59, 56]
[harit Gupta, Rahul Gupta, Shikha kom]
[59495.0, 35695.0, 59565.0]
但是当输入不正确时,这将无限次进行。
amehla@slbnen3000pc50:~/Desktop/test$ java People
Enter number of people
3
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") :
Rahul Gupta
Now putting name
Enter your age:
59
Now putting age
Enter your annual income:
fasdf
Error !! Wrong input, Please try again.
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") :
Rahul Gupta
Now putting name
Enter your age:
Error !! Wrong input, Please try again.
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") :
Rahu lasdf
Now putting name
Enter your age:
Error !! Wrong input, Please try again.
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") :
asdf adsf
Now putting name
Enter your age:
Error !! Wrong input, Please try again.
0Enter your last and first name (for e.g. if full name is "Rahul Gupta" then enter "Gupta Rahul") :
^Camehla@slbnen3000pc50:~/Desktop/test$ exit
exit
Script done on Sat 03 Feb 2018 07:57:22 PM NST
会很有帮助,即使有人回复。
谢谢。
【问题讨论】:
-
你到底为什么要调用一个变量“InputMismatchException”(大写)?
-
在你的 catch 中添加一个
in.nextLine();可能会解决它。
标签: java exception while-loop try-catch