2017-03-11 1 views
-1

私はここでこれを調べようとしましたが、私が探している正確な解決策がまだ見つかりませんでした。数学は間違って出てくる。平均値がインコストを出しています

 //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はすべてエラーとして表示されます。どのようにそれを修正する手がかりがない。

+1

ようこそ:

は、次のように変更して、あなたのwhileループを交換してください!デバッガの使い方を学ぶ必要があるようです。 [補完的なデバッグ手法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)にご協力ください。その後も問題が残っている場合は、もう少し詳しくお聞かせください。 –

+0

コードをデバッグするだけで、プログラマーが実際にあなたの最後の入力を気にかけていないのに気付くでしょう。 – Tom

+0

@JoeBurkhart 2つのこと:1)あなたはグレードの1つを欠いている(最後のもの)。 2)あなたは整数除算を使用しています。 –

答えて

0

入力を取得する前に合計を実行しています。それ以降はやるべきだ。スタックオーバーフローへ

 while (count < numberGrades){ 

      System.out.print ("Please enter your next grade: "); 
      currentGrade = scan.nextInt(); 
      totalGrade += currentGrade; 
     count++; 
     } 
+0

うわー。ありがとうございました。それは単純なことだと思っていて、他のすべてのものを動かそうと何時間もやっていて、コードを見つめた後に私はちょっと見落としている。 –

関連する問題