简体   繁体   中英

Java how to compute average scores using loops

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:

Student 1

Enter your score: 100

Enter your score: 90

Enter your score: 80

Enter your score: 70

Enter your score: -1 * once the student enters -1 it should compute average

Average Score = 85.

Student 2

Enter your score: 90

ETC

ETC

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.

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;
          }   

      }  



}



}

You haven't explicitly asked a question so I'll try and comply to the "set me in the right direction" part.

I'd suggest re-formatting the loop structure to a cleaner one, like this:

double total;
for(int student = 1; student <= 4; student++) {
    System.out.printf("Student %d\n", student);
    double sum = 0, count = 0;

    while(true) {
        System.out.printf("Enter your score: ");
        double input = scanner.nextDouble();
        if(input == -1) break;
        sum += input;
        count++;
    }
    total += sum;

    System.out.printf("Average: %.2f\n", sum / count);
}

System.out.printf("Total: %.2f\n", total);

Hope that's enough to give you some pointers.

edit: forgot to take care of total

So, you wish to iteratively go through all the input and just remember the maximum one. Make an integer variable max and after each student, just change it if needed. (It's zero by default jn Java) As for the calculation for each student, you shouldn't be checking for the failed " score != - 1" condition in each iteration. Instead, you should do the final calculations after the while loop. (average, possible update of the maximum, resetting the variables, etc. ) You also need the outer loop (in the stated code, these calculations are done for one student only) which you would control in a different manner.

Also, if you need to use only 4 grades, you might want to consider using the for loop.

You can try with this :D

public static void main (String[] args) throws java.lang.Exception
{
    double average = 0;
    double i = 0;
    int student = 0;
    boolean flag = true;
    Scanner input = new Scanner(System.in);
    while(flag)
    {
        System.out.printf("Student: ");
        System.out.println(student);
        System.out.print("Enter Your Score: ");

        double score = input.nextDouble();
        if(score!=-1){
            average=average+score;
            i=i+1;
        }

        if(score==-1){
            System.out.printf("Average: ");
            System.out.println(average/i);
            //reset values
             average = 0;
             i = 0;
            student=student+1;
        }
        if(score==-2){
            //you need break the while in some moment.
            flag = false;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM