title: Java how to compute average scores using loops
body: <p>I need to write a program in Java that computes the average score for 4 students. The student will put in their 4 scores, and once they are done they will input -1 to compute the average. Once this is done the program needs to move onto student 2 and so on. At the end it is supposed to display the highest average from the 4 student average test scores. Here is what it should look like when it is run:</p>
  
  <p>Student 1</p>
  
  <p>Enter your score: 100</p>
  
  <p>Enter your score: 90</p>
  
  <p>Enter your score: 80</p>
  
  <p>Enter your score: 70</p>
  
  <p>Enter your score: -1 * once the student enters -1 it should compute average</p>
  
  <p>Average Score = 85.</p>
  
  <p>Student 2</p>
  
  <p>Enter your score: 90</p>
  
  <p>ETC</p>
  
  <p>ETC</p>
  
  <p>The problem with my code is that the average is only correct for the first student. When I input -1 to get the average for the second student, the calculation is incorrect. We are only allowed to use loops. The only hints I was given were that we are supposed to write an outer loop that iterates 4 times, write an inner loop that loops as long as the student has scores to enter, inside the inner loop prompt the user to enter their last score or -1 to compute average. I don't want you guys to do the project for me but to just set me in the right direction. I feel like I am not using the right loop.</p>
  
  <pre><code>import java.util.Scanner;
  
  public class TestScore
  
  {
  
       public static void main(String[]args)
       {
  
         double score = 0;
  
         double totalScore = 0;
  
         double count = 0;
  
         double average = 0;
  
         Scanner input = new Scanner(System.in);
  
  
        System.out.println("Student 1");
        System.out.printf("Enter Your Score: ");
        score = input.nextDouble();
  
  
  
        while (score != -1){
            System.out.printf("Enter Your Score: ");
            totalScore = totalScore + score;
            score = input.nextDouble();
            count++;
            average = totalScore / count;
  
            if (score == -1){
                System.out.printf("Average Score = %.2f\n ",average);
                count = 0;
                score = 0;
                totalScore = 0;
                average = 0;
                System.out.println("Student 2");
                System.out.printf("Enter Your Score: ");
                score = input.nextDouble ();
                count++;
                average = totalScore / count;
            }   
  
        }  
  
  
  
  }
  
  
  
  }
  </code></pre>