问题:某比赛五位评委打分,分数由键盘录入,选手分数的计算规则为:去掉一个最高分,去掉一个最低分,然后计算平均值,所得的平均分为选手最终成绩。
import java.util.Scanner; public class Homework02 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //评委打分,五位评委打分,分数由键盘录入,去掉一个最高分,去掉一个最低分,然后计算平均值,所得的平均分为选手最终成绩 judgesScores(sc); } //评委打分,五位评委打分,分数由键盘录入,去掉一个最高分,去掉一个最低分,然后计算平均值,所得的平均分为选手最终成绩 public static void judgesScores(Scanner sc){ int[] score = new int[5]; gradesIn(sc,score); //平均值 double average1 = (scoreAdd(score) - maxNum(score) - minNum(score))/(score.length-2); System.out.println("选手的最终得分是:"+average1); } //成绩输入 public static int[] gradesIn(Scanner sc, int[] score){ System.out.println("请输入成绩:"); for(int i = 0;i < score.length;i++){ int num = sc.nextInt(); score[i] = num; } System.out.println("输入的成绩分别是:"); for(int i = 0;i < score.length;i++){ System.out.print(score[i] + " "); } System.out.println(); return score; } //求成绩之和 public static int scoreAdd(int[] score){ int scoreAdd1 = 0; for(int i = 0;i < score.length;i++){ scoreAdd1 += score[i]; } return scoreAdd1; } //求数组中的最大值 public static int maxNum(int[] arr1){ //求最大值 int maxNum1 = arr1[0]; for(int i = 1; i < arr1.length ;i++){ if(arr1[i] > maxNum1){ maxNum1 = arr1[i]; } } return maxNum1; } //求数组中的最小值 public static int minNum(int[] arr1){ //求最小值 int minNum1 = arr1[0]; for(int i = 1; i < arr1.length ;i++){ if(arr1[i] < minNum1){ minNum1 = arr1[i]; } } return minNum1; } }