Java 数组循环输入 8 个学生成绩并计算总分、平均分、最高分和最低分
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] scores = new int[8]; // 声明一个长度为 8 的 int 型数组
// 循环接收学生的成绩
for (int i = 0; i < scores.length; i++) {
System.out.print("请输入第" + (i + 1) + "个学生的成绩:");
scores[i] = scanner.nextInt();
}
int sum = 0; // 总分
int max = scores[0]; // 最高分,初始值为第一个学生的成绩
int min = scores[0]; // 最低分,初始值为第一个学生的成绩
// 计算总分,最高分和最低分
for (int score : scores) {
sum += score;
if (score > max) {
max = score;
}
if (score < min) {
min = score;
}
}
double average = (double) sum / scores.length; // 平均分
// 输出总分,平均分,最高分和最低分
System.out.println("总分:" + sum);
System.out.println("平均分:" + average);
System.out.println("最高分:" + max);
System.out.println("最低分:" + min);
}
}
原文地址: https://www.cveoy.top/t/topic/nZJh 著作权归作者所有。请勿转载和采集!