0
我試圖讓機器輸入錯誤數據時輸出錯誤消息。當用戶提供Java中的多用戶輸入錯誤消息(while循環)
- 或者非數字值,或
- 負值兩個類型的錯誤消息被顯示。
如果用戶輸入非數字值但不是負值,我只能打印出錯信息。如果用戶輸入錯誤的分數,我想編寫用於打印錯誤消息的代碼,但我仍然停留在步驟1.我應該在哪裏將代碼放在以下程序中?
import java.util.Scanner;
public class lab7errortesting {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of grades: ");
if (!input.hasNextDouble()) {
System.out.println("***Error: Entered a non-numeric data. Please enter numeric data.");
input.next();
} else {
double random = input.nextDouble();
double min = Integer.MIN_VALUE;
double max = Integer.MAX_VALUE;
double total=0;
int count = 1;
while (count < random+1) {
System.out.println("Enter grade " + count + ":");
double grade = input.nextDouble();
total+=grade;
count++;
if (max > grade){
max = grade;
}
if (min < grade){
min = grade;
}
}
System.out.println("Total is " + total);
System.out.println("Average is " + (total/random));
System.out.println("Smallest number is " + max);
System.out.println("Largest number is " + min);
}
}
}