/** *功能:统计一组学生成绩 *作者:许锐 *创作时间:2022年3月14日 */ public class Task04 { public static void main(String[] args) { //声明部分 double[] scores = new double[10]; //双精度一维数组,10个元素,scores[0] , scores[1],..., scores[9] double sum, average, max, min; Scanner sc=new Scanner(System.in); //输入部分 for (int i = 0; i < scores.length; i++) { while (true) { System.out.print("scores[" + i + "] = "); scores[i] = sc.nextDouble(); //判断成绩是否合法 -[0,100] if (scores[i] >= 0 && scores[i] <= 100) { break; //跳出while循环 }else { //提示用户输入成绩不合法 System.out.println("温馨提示:成绩必须在[0,100]之间!"); } } } //处理部分 sum = 0; for (int i = 0; i < scores.length; i++) { sum = sum + scores[i]; } average = sum / scores.length; max = Double.MIN_VALUE; for (double score : scores) { if (max < score) { max = score; } } min = Double.MAX_VALUE; for (int i = 0;i < scores.length; i++){ if (min > scores[i]) { min = scores[i]; } } //输出部分 System.out.println("sum = " + sum); System.out.println("average = " + average); System.out.println("max = " + max); System.out.println("min = " + min); } }
统计学生成绩
于 2022-04-14 11:20:13 首次发布