java的7个评委给5位选手打分每位选手去掉评委的最高分和最低分 最后得出选手的最后的平均值,并且打印出小票,获得全部选手 的得分信息标注前三名选手
时间: 2024-06-05 21:06:31 浏览: 81
参赛结果分数进行处理
import java.util.*;
public class Score {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] scores = new int[5][7];
for (int i = 0; i < 5; i++) {
System.out.println("请输入第" + (i + 1) + "位选手的7个评委分数:");
for (int j = 0; j < 7; j++) {
scores[i][j] = sc.nextInt();
}
}
System.out.println("所有选手的得分信息如下:");
int[] averages = new int[5];
for (int i = 0; i < 5; i++) {
int sum = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int j = 0; j < 7; j++) {
sum += scores[i][j];
max = Math.max(max, scores[i][j]);
min = Math.min(min, scores[i][j]);
}
averages[i] = (sum - max - min) / 5;
System.out.println("第" + (i + 1) + "位选手的分数为:" + Arrays.toString(scores[i]));
System.out.println("去掉最高分" + max + "和最低分" + min + "后,该选手的平均分为:" + averages[i]);
}
System.out.println("前三名选手的得分信息如下:");
int[] sortedAverages = Arrays.copyOf(averages, averages.length);
Arrays.sort(sortedAverages);
for (int i = 4; i >= 5 - Math.min(3, sortedAverages.length); i--) {
int index = getIndex(averages, sortedAverages[i]);
System.out.println("第" + (index + 1) + "位选手的平均分为:" + sortedAverages[i]);
System.out.println("该选手的分数为:" + Arrays.toString(scores[index]));
}
}
public static int getIndex(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
}
阅读全文