简体   繁体   中英

Displaying the highest average of bowling score

I'm having trouble making the code display the highest average of the four averages, I seem to only get the last average score and that fills in for the highest average which it is not the highest score.

Here is my code:

 import java.io.*;
    import java.util.*;

    class Bowling Scores
    {

    public static void main (String args[])

    {
      Scanner keyboard = new Scanner(System.in);
       int G =0;
       int scores = 0;
       double avg = 0;
       double highest_average=0;
       int times = 0;
       int players = 4;

      for(int i =1; i<=players; i++) {
         System.out.println("Player " + i);
          if (avg > highest_average) 
            highest_average= avg; 
              do
         {
            System.out.println("Enter your bowling score:");
            G = keyboard.nextInt();
            scores += G;
            times++;
         }
         while((G !=-1));  // -1 is what makes the loop end for the current player, a total of four players will get an average after -1 is inputted
         scores += + 1;
         times = times - 1; 
         avg = (scores)/(times);
         System.out.printf("Average= %.2f\n", avg);
         scores =0; times =0; 
         System.out.println("");
      }

         System.out.println("Highest average:" + highest_average);

     }

    }

Here is a sample run of the code:

 Player 1

 Enter your bowling score:

 300

 Enter your bowling score:

 222

 Enter your bowling score:

 -1

 Average= 261.00

 Player 2

 Enter your bowling score:

 210

 Enter your bowling score:

 211

 Enter your bowling score:

 300

 Enter your bowling score:

 -1

 Average= 240.00

 Player 3

 Enter your bowling score:

 233

 Enter your bowling score:

 -1

 Average= 233.00

 Player 4
 Enter your bowling score:

 112

 Enter your bowling score:

 -1

 Average= 112.00

 Highest average:112.0 // this is not the highest score.

You never update Highest Avg for each player, only for the last player. That's why you're always using the last person's avg.

Just move your if statement inside the for loop and it should work fine. Make sure to leave your print statement outside the loop so that you only print the highest avg once at the end.

for (int i = 1; i <= players; i++) {
    System.out.println("Player " + i);
    do {
        System.out.println("Enter your bowling score:");
        G = keyboard.nextInt();
        scores += G;
        times++;
    } while ((G != -1)); // -1 is what makes the loop end for the current player, a total of four players will get an average after -1 is inputted

    scores += +1;
    times = times - 1;
    avg = (scores) / (times);
    System.out.printf("Average= %.2f\n", avg);
    scores = 0;
    times = 0;
    System.out.println("");

    if (avg > highest_average) {
        highest_average = avg;
    }
}
System.out.println("Highest average:" + highest_average);
if (avg > highest_average) {           
     highest_average= avg;

This code should be in your for-loop, otherwise the highest average never gets updated.

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