JAVA输入多个成绩,统计平均分、最高分、最低分、总考试人数、各级别人数、各级别人数占总考试人数的百分比
时间: 2023-11-04 19:04:58 浏览: 167
以下是一种可能的Java程序实现:
import java.util.Scanner;
public class GradeStatistics {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int total = 0; // 总考试人数
int numA = 0, numB = 0, numC = 0, numD = 0, numF = 0; // 各级别人数
double sum = 0.0; // 总成绩
double max = Double.MIN_VALUE, min = Double.MAX_VALUE; // 最高分、最低分
System.out.print("请输入考试人数:");
int n = input.nextInt();
for (int i = 1; i <= n; i++) {
System.out.print("请输入第" + i + "个学生的成绩:");
double score = input.nextDouble();
if (score >= 90) {
numA++;
} else if (score >= 80) {
numB++;
} else if (score >= 70) {
numC++;
} else if (score >= 60) {
numD++;
} else {
numF++;
}
sum += score;
total++;
if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
}
double average = sum / total; // 平均分
double percentA = numA * 100.0 / total; // A级百分比
double percentB = numB * 100.0 / total; // B级百分比
double percentC = numC * 100.0 / total; // C级百分比
double percentD = numD * 100.0 / total; // D级百分比
double percentF = numF * 100.0 / total; // F级百分比
System.out.println("平均分:" + average);
System.out.println("最高分:" + max);
System.out.println("最低分:" + min);
System.out.println("总考试人数:" + total);
System.out.println("A级人数:" + numA + ",占比" + percentA + "%");
System.out.println("B级人数:" + numB + ",占比" + percentB + "%");
System.out.println("C级人数:" + numC + ",占比" + percentC + "%");
System.out.println("D级人数:" + numD + ",占比" + percentD + "%");
System.out.println("F级人数:" + numF + ",占比" + percentF + "%");
}
}
该程序首先使用Scanner类从标准输入读取考试人数,然后使用for循环依次读取每个学生的成绩,同时统计各级别人数、总成绩和最高分、最低分。最后计算平均分和各级别人数占总考试人数的百分比,并输出结果。