.用java统计全班学生成绩。输入学生的学号及语文、数学、英语成绩,输出学生各科成绩信息、平均成绩和总成绩。(二维数组及循环) 请输入本班学生总数:2学生成绩结果如下: ———————————————
时间: 2023-11-12 16:03:36 浏览: 217
是用Java统计全班学生成绩的代码,其中使用了二维数组和循环来实现输入、计算和输出学生成绩信息、平均成绩和总成绩。
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入本班学生总数:");
int n = input.nextInt();
String[] subjects = {"语文", "数学", "英语"};
int[][] scores = new int[n][3];
int[] sum = new int[n];
double[] avg = new double[n];
for (int i = 0; i < n; i++) {
System.out.print("请输入第" + (i + 1) + "个学生的学号:");
String id = input.next();
System.out.println("请输入第" + (i + 1) + "个学生的成绩:");
for (int j = 0; j < 3; j++) {
System.out.print(subjects[j] + ":");
scores[i][j] = input.nextInt();
sum[i] += scores[i][j];
}
avg[i] = sum[i] / 3.0;
}
System.out.println("学号\t语文\t数学\t英语\t总分\t平均分");
for (int i = 0; i < n; i++) {
System.out.print((i + 1) + "\t");
for (int j = 0; j < 3; j++) {
System.out.print(scores[i][j] + "\t");
}
System.out.print(sum[i] + "\t");
System.out.printf("%.2f", avg[i]);
System.out.println();
}
System.out.println("全班每门课程的平均分:");
for (int j = 0; j < 3; j++) {
int sumSubject = 0;
for (int i = 0; i < n; i++) {
sumSubject += scores[i][j];
}
double avgSubject = sumSubject / (double) n;
System.out.print(subjects[j] + ":" + avgSubject + "\t");
int[] count = new int[5];
for (int i = 0; i < n; i++) {
if (scores[i][j] < 60) {
count[0]++;
} else if (scores[i][j] < 70) {
count[1]++;
} else if (scores[i][j] < 80) {
count[2]++;
} else if (scores[i][j] < 90) {
count[3]++;
} else {
count[4]++;
}
}
System.out.println("不及格:" + count[0] + " 60-69:" + count[1] + " 70-79:" + count[2] + " 80-89:" + count[3] + " 90-100:" + count[4]);
}
System.out.println("不及格学生信息:");
for (int i = 0; i < n; i++) {
boolean isFail = false;
for (int j = 0; j < 3; j++) {
if (scores[i][j] < 60) {
isFail = true;
break;
}
}
if (isFail) {
System.out.print((i + 1) + "\t");
for (int j = 0; j < 3; j++) {
System.out.print(scores[i][j] + "\t");
}
System.out.print(sum[i] + "\t");
System.out.printf("%.2f", avg[i]);
System.out.println();
}
}
System.out.println("优等生信息:");
for (int i = 0; i < n; i++) {
boolean isExcellent = true;
for (int j = 0; j < 3; j++) {
if (scores[i][j] < 90) {
isExcellent = false;
break;
}
}
if (isExcellent) {
System.out.print((i + 1) + "\t");
for (int j = 0; j < 3; j++) {
System.out.print(scores[i][j] + "\t");
}
System.out.print(sum[i] + "\t");
System.out.printf("%.2f", avg[i]);
System.out.println();
}
}
}
}
```
阅读全文