统计一组学生成绩
- 统计一组学生成绩的总分、平均分、最高分和最低分
(一)编程实现
-
在
net.nieyourong.p02.t07
包里创建Task07
-
运行程序,查看结果
package net.nieyourong.p02.t07;
import java.util.Scanner;
/**
* 功能:统计一组学生成绩
* 作者:聂友蓉
* 日期:2022年04月14日
*/
public class Task07 {
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++) {
System.out.print("scores[" + i + "] = ");
scores[i] = sc.nextDouble();
}
//处理部分
sum = 0;
for (int i = 0; i <scores.length; i++) {
sum = sum + scores[i];
}
average = sum / scores.length;
max = Double.MIN_VALUE;
for (int i = 0; i < scores.length; i++) {
if (max < scores[i]) {
max = scores[i];
}
}
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);
}
}