Java入门题-计算平均成绩、总分、最高/低分

发布时间 2023-08-10 17:04:04作者: Lee597

题目:输入8位学生的成绩,计算总分、平均分、最高分、最低分

 

重点:使用数组、循环、四舍五入

 

代码:引用 import java.util.Scanner; 

 

int[] student_soure = new int[8];
for (int i = 0; i < student_soure.length; i++) {
System.out.println("请输入第" + i + "位学生的成绩:");
Scanner scanner = new Scanner(System.in);
student_soure[i] = scanner.nextInt();
}
int all_soure = 0;
int max_soure = student_soure[0];
int min_soure = student_soure[0];
for (int j : student_soure) {
all_soure += j;
if (j > max_soure) {
max_soure = j;
}
if (j < min_soure) {
min_soure = j;
}
}
System.out.println(student_soure.length+"位学生的总分为:"+all_soure);
System.out.printf(student_soure.length+"位学生的均分为:"+ "%.2f",(double)all_soure/student_soure.length);
System.out.println("\n" + student_soure.length+"位学生中的最高分为:"+max_soure);
System.out.println(student_soure.length+"位学生中的最低分为:"+min_soure);