鍵盤接收7位評委打的分數,去掉一個最高分,去掉一個最低分,最后得分
Scanner scanner = new Scanner(System.in); // 定義一個數組,存儲 7 個評委的打分 int[] scores = new int[7]; for (int i = 0; i < scores.length; i++) { System.out.println("請 " + (i + 1) + " 號評委打分:"); int number = scanner.nextInt(); scores[i] = number; } int max = scores[0]; int min = scores[0]; // 最高分和最低分的索引,同步最大最小值的變化 int maxIndex = 0; int minIndex = 0; int sum = scores[0]; for (int i = 1; i < scores.length; i++) { int j = scores[i]; if (max < j) { // 如果最大值小於當前元素的值,那么更新當前元素為最大值 max = j; maxIndex = i; } if (min > j) { // 如果最小值大於當前元素的值,那么更新當前元素為最小值 min = j; minIndex = i; } sum += j; } System.out.println("去掉 " + (maxIndex + 1) + " 號評委的最高分:" + max); System.out.println("去掉 " + (minIndex + 1) + " 號評委的最低分:" + min); int avg = (sum - max - min) / (scores.length - 2); System.out.println("選手最終得分:" + avg + " 分");