Java 程序计算平均值。如何显示最高平均值?

Java program calculates averages. How do I display highest average?

我有一个 java 程序,它获取 4 个玩家的分数,并在输入 -1 时计算每个玩家的平均分数。在程序结束时,我需要显示 4 个平均分中最高的平均分,但是我不知道该怎么做,你们能给我指明正确的方向吗?该程序在 运行 时看起来像这样:(我还附上了我的代码)我只能使用循环。

玩家 1

输入你的分数:100

输入你的分数:90

输入你的分数:80

输入你的分数:70

输入你的分数:-1(输入-1时,计算平均分)

平均分 = 85.00

玩家 2

输入你的分数:90

输入你的分数:90

等等

等等

import java.util.Scanner;

public class BowlingScores
{
    public static void main(String[]args)
    {
      double score = 0;
      double totalScore = 0;
      double count = 0;

      Scanner input = new Scanner(System.in);

      for (int player = 1; player <= 4; player++){
         System.out.printf("\nPlayer %d\n", player);

        do{
           System.out.println("Enter Your Score: ");
           totalScore = totalScore + score;
           score = input.nextDouble(); 
           count++;
         } while (score != -1);

         if (score == -1){
          count--;
          System.out.printf("Average Score = %.2f\n",totalScore/count);
          count = 0;
          score = 0;
          totalScore = 0;
         }    
      }    
   }
}

您可以像这样在代码顶部添加最高值的副本 double highest = 0 然后在您的 for 循环中有一种方法可以在当前平均值更高的情况下替换它。

if(score > highest){
      highest=score;
}

然后在 -1 处您可以将其重置为 0 或继续运行。

只需跟踪另一个变量中的最高平均值。然后在您的 score == -1 条件中检查当前平均值是否高于最高平均值,如果是,则将当前值设置为新的最高值。然后在你的 for 循环之后你可以打印出最高的平均值。

取所有 4 名玩家的平均值并存储到一个数组中。然后检查数组中最大的数字。

double[] highest = new double[4];
for(int i=0; i<4; i++){
    highest[i] = totalScore/count;
}

if(highest[0]>highest[1] && highest[0]>highest[2] && highest[0]>highest[3])
    System.out.println("highest" + highest[0]);

else if(highest[1]>highest[0] && highest[1]>highest[2] && highest[0]>highest[3])
    System.out.println("highest" + highest[1]);

else if(highest[2]>highest[0] && highest[2]>highest[1] && highest[0]>highest[3])
    System.out.println("highest" + highest[2]);

else System.out.println("highest" + highest[3]);
public static void main(String[] args) {
    final List<Double> allAverages = new ArrayList<>();
    double score = 0;
    double totalScore = 0;
    double avg = 0;
    int count = -1;

    Scanner input = new Scanner(System.in);

    for (int player = 1; player <= 4; player++) {
        System.out.printf("\nPlayer %d\n", player);

        do {
            System.out.println("Enter Your Score: ");
            totalScore = totalScore + score;
            score = input.nextDouble();
            count++;
        } while (score != -1);

        avg = totalScore / count;
        System.out.printf("Average Score = %.2f\n", avg);
        allAverages.add(totalScore / count);
        score = 0;
        totalScore = 0;
        count = -1;
    }

    System.out.println("The highest is : " + Collections.max(allAverages));

}

这里列出了我对您的代码所做的修改以使其正常工作:

  • 变量 count 现在从 -1 开始,所以当我们退出循环时不需要执行 count--。
  • 我添加了一个列表来跟踪所有平均值。
  • 我在循环后删除了验证 if(count == -1),因为退出循环的唯一方法是让 count == -1 所以我们已经知道它是实际值。
  • 为了检索所有平均值的最大值,我使用 Collections.max(allAverages)
public static void main(String[]args) {
    double bestScore = 0;
    int bestPlayer = 1;

    Scanner input = new Scanner(System.in);

    for (int player = 1; player <= 4; player++){
        System.out.printf("\nPlayer %d\n", player);
        int count = 0; // does not need to be double
        double totalScore = 0;
        do {
            System.out.println("Enter Your Score: ");
            double score = input.nextDouble();
            if (score != -1) { 
                totalScore += score;
                count++;
            }
        } while (score != -1);

        double averageScore = totalScore / count;

        if (averageScore > bestScore) {
            bestScore = averageScore;
            bestPlayer = player;
        }

        System.out.printf("Average Score = %.2f\n", averageScore);
    }

    System.out.printf("Best Average Score = %.2f, best player = %d\n", bestScore, bestPlayer);
}

只需保留一个变量来记录每次的最高平均分,如下例中的topAverage。代码与您的代码相同,只是为了便于阅读我更改了变量名称。

public static void main(String[] args) throws NumberFormatException,
            IOException {
        double currentScore = 0;
        double totalScore = 0;
        int numScores = 0;
        double averageScore = 0;
        double topAverage = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for (int i = 0; i < 4; i++) {

            do {
                System.out.println("Enter Score");
                currentScore = Double.parseDouble(br.readLine());
                totalScore = totalScore + currentScore;
                numScores += 1;
            } while (currentScore != -1);
            numScores-=1;
            totalScore+=1;
            averageScore = totalScore / numScores;
            System.out.println("Average Score is " + averageScore);
            if (topAverage < averageScore) {
                topAverage = averageScore;
            }
            currentScore = 0;
            totalScore = 0;
            numScores = 0;
        }
        System.out.println("Top Average is " + topAverage);
    }

编辑

@Andrei Nikolaenko 在我之前发布了相同的答案,所以请考虑这个答案,我添加此编辑是因为我想提及我的浏览器没有按时刷新以显示我知道答案已经发布了。