0
我的程序出現問題。一切都運行平穩,但是,當用戶輸入錯誤的變量時,它顯示正確的反饋,但用戶必須輸入一個額外的變量。Java程序使用戶在初始輸入錯誤後輸入兩次相同的輸入以運行
也許這是一個簡單的錯誤我已經作出,但我不能看到它..
這是令人困惑的。一個例子,當我運行該程序:
How many grades would you like to average?
3
Please enter 3 grades:
90
jf //Intentional user input error
You've entered a non-numerical variable. Please enter a number:
95
100 //The program should go ahead and calculate the average after this is entered
100 //It should not expect this fourth input if the amount of grades is 3
Your average is: 96.67
在控制檯中的第二輸入100不應出現,但是當用戶具有至少一個輸入錯誤它。如果我要運行程序並輸入所有正確的變量,那麼程序運行平穩。
當詢問用戶想要平均分多少個等級時,也會發生此錯誤。我認爲第二部分可以更容易地查看我的程序出了什麼問題。
我試圖讓這個程序順利運行。幫助表示讚賞!
for (gradesCount = 0; gradesCount < gradeNumber; gradesCount++) {
// Check if the input variables are numerical variables
while (!input.hasNextDouble()) {
input.next();
System.out.println("You've entered a non-numerical variable. Please enter a number: ");
while (input.nextInt()<= 0){
System.out.println("You've entered a negative number. Please eneter a positive number: ");
}
}
// Read grade input
gradesInput = input.nextDouble();
謝謝您的輸入!然而,我的程序的目的之一是它爲用戶提供了有關初始輸入錯誤原因的反饋。我只想弄清楚爲什麼用戶輸入錯誤後需要輸入兩次。 – Isabelle
檢查'input [i]> 0'是否打印不接受輸入的原因,而不是輸入無效。 ''打印'不是數字' – Saravana
從掃描儀你正在採取'input.next()'和'input.nextInt()'。你可以得到'input.nextInt()'或'input.next() – Saravana