【发布时间】:2014-02-26 16:03:58
【问题描述】:
要求:
接受 10 个数字,将它们输入到一个数组中,然后调用一个方法来计算并返回最小值。该程序被认为是防错的,因此当用户输入无效条目时,它会通知用户并重新提示。我正在尝试使用try catch,但是当输入无效条目(即字符)时,扫描仪不会重新提示。
有什么想法吗?
试过了:
//Variables
double [] doubleArray = new double[10];
Scanner input = new Scanner(System.in);
//Prompt
System.out.println("This program will prompt for 10 numbers and display the smallest of the group");
//Get values
for (int i = 0; i < doubleArray.length; i++) {
try {
System.out.println("Please enter entry "+ (i+1));
doubleArray[i] = input.nextDouble();
} catch (InputMismatchException e) {
// TODO: handle exception
System.out.println("Please enter a rational number");
i--;
}
}
//Invoke method and display result
System.out.println("The smallest value is: "+index(doubleArray));
【问题讨论】: