[Java] 裁判打分(最值问题 Max Min)

问题

  • 5个裁判员打分,去掉一个最高分,去掉一个最低分,剩余三个分数的平均值就是该选手的最终打分;
  • 已知每个裁判打分都是从 {10,20,30,40,50,60,70,80,90,100} 数组中随机抽取数据给选手打分,在控制台打印出5个裁判给该选手的打分情况和选手的最终打分;

Java 实例

import java.util.Random;

public class test {
    public static void main(String args[]) {
        System.out.println("5个裁判给选手的打分情况为:");
        int score[] = new int[5];
        Random r = new Random();
        for (int i = 0; i <= 4; i++) {
            score[i] = 10 * (r.nextInt(10) + 1);
            System.out.print(score[i] + ",");
        }
        System.out.println();
        System.out.println("选手的最终得分为:" + average(score));
    }

    static int average(int[] a) {
        int sum = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;

        for (int i = 0; i < a.length; i++) {
            sum += a[i];
            if (a[i] > max)
                max = a[i];
            if (a[i] < min)
                min = a[i];
        }
        return (sum - max - min) / (a.length - 2);
    }
}

你可能感兴趣的:([Java] 裁判打分(最值问题 Max Min))