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

时间:2015-02-21 20:59:51

标签: java loops average mean

我有一个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;
         }    
      }    
   }
}

6 个答案:

答案 0 :(得分:0)

您可以在代码顶部添加最高级别的副本,例如double highest = 0,然后在for循环中,如果当前平均值更高,则可以替换它。

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

取所有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]);

答案 3 :(得分:0)

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开始,因此当我们退出循环时,不需要进行计数。
  • 我添加了一个跟踪所有平均值的列表。
  • 我已经在循环后删除了验证if(count == -1),因为退出循环的唯一方法是使count == -1,所以我们已经知道它是实际值。
  • 要检索所有平均值的最大值,我使用Collections.max(allAverages)

答案 4 :(得分:0)

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

答案 5 :(得分:0)

在下面的示例中,只需保留一个变量以记录最高平均分数,如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 在我面前发布了相同的答案,所以考虑一下这个答案,我正在添加此编辑,因为我想提一下我的浏览器没有准时刷新,告诉我答案已经已经发布了。