package com.fqs.test; import java.util.Random; import java.util.Scanner; public class hello { public static void main(String[] args) { //评委打分 评委个数6个 //分数的范围1到100 //去掉最高分 去掉最低分 剩下的4个评委的平均分 double[] arr = new double[6]; Scanner sc = new Scanner(System.in); for (int i = 1; i <= arr.length; i++) { System.out.println("请第" + i + "位评委给评分"); double num = sc.nextInt(); boolean flag = true;//是否有不合法的评分 如果不合法一直循环让输入 只有true才能进到循环里 while (flag) { if (num < 0 || num > 100) { flag = true; System.out.println("不合法,请输入1到100之间的评分"); num = sc.nextInt(); } else { flag = false; arr[i - 1] = num;//合法的存到arr数组中 System.out.println("合法,放到arr数组下标:" + (i - 1)); break; } } } System.out.println("avg:" + getAvg(arr)); } public static double getAvg(double[] arr) { double max = arr[0]; double min = arr[0]; double avg = 0; double sum = 0; for (int i = 0; i < arr.length; i++) { if (max < arr[i]) { max = arr[i]; //获得最大值 } } for (int i = 0; i < arr.length; i++) { if (min > arr[i]) { min = arr[i]; //获得最小值 } } for (int i = 0; i < arr.length; i++) { sum = sum + arr[i]; } System.out.println("sum:" + sum); avg = (sum - min - max) / (arr.length - 2); return avg; } }//类结束
标签:arr,int,double,sum,最高分,System,76,去掉 From: https://www.cnblogs.com/haha1988/p/17467695.html