私はここでこれを調べようとしましたが、私が探している正確な解決策がまだ見つかりませんでした。数学は間違って出てくる。平均値がインコストを出しています
//Local Constants
int count = 0; //Used to track the number of grades entered by the user
//Local Variables
double currentGrade = 0; //User's current grade inputed
double numberGrades; //Total number of grades to be entered
double totalGrade = 0; //Total of all grades
double gradeAverage; //Average of all grades
//Main Function
//Ask the user for the amount of grades they would like to enter
System.out.print ("How many grades would you like to enter? ");
numberGrades = scan.nextInt();
//If the user asks to enter 0 numbers, output an error
while (numberGrades <= 0){
System.out.print ("Please enter a valid number! ");
numberGrades = scan.nextInt();
}
//Add the grades together as they are input by the user
while (count < numberGrades){
totalGrade += currentGrade;
System.out.print ("Please enter your next grade: ");
currentGrade = scan.nextInt();
count++;
}
//Calculate and output the average to the user
gradeAverage = (totalGrade/numberGrades);
System.out.print ("\n");
System.out.print ("The average of all grades is: " + gradeAverage);
}
}これは修正されました
と思ったが、私はそれを実行したとき、私はまだ同じ問題を取得しています。私が試した例では3つの数字を入力し、それぞれ50だった。平均は33.0になった。私はデバッガを実行した後、最初の番号が入力されるとエラーが表示され続けます。 count、totalGrade、currentGradeはすべてエラーとして表示されます。どのようにそれを修正する手がかりがない。
ようこそ:
は、次のように変更して、あなたの
while
ループを交換してください!デバッガの使い方を学ぶ必要があるようです。 [補完的なデバッグ手法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)にご協力ください。その後も問題が残っている場合は、もう少し詳しくお聞かせください。 –コードをデバッグするだけで、プログラマーが実際にあなたの最後の入力を気にかけていないのに気付くでしょう。 – Tom
@JoeBurkhart 2つのこと:1)あなたはグレードの1つを欠いている(最後のもの)。 2)あなたは整数除算を使用しています。 –