用JAVA写以下代码:统计全班学生成绩。输入学生的学号及语文、数学、英语成绩,输出学生各科成绩信息、平均成绩和总成绩。(二维数组及循环)
时间: 2024-06-13 07:06:43 浏览: 237
程序用JAVA编写,统计每个学生的总分
以下是用JAVA实现统计全班学生成绩的代码,其中使用了二维数组和循环:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入班级人数:");
int n = scanner.nextInt();
String[][] students = new String[n][4];
double[] scores = new double[n];
double sum = 0;
int maleCount = 0;
int femaleCount = 0;
for (int i = 0; i < n; i++) {
System.out.print("请输入第" + (i + 1) + "个学生的姓名:");
students[i][0] = scanner.next();
System.out.print("请输入第" + (i + 1) + "个学生的性别(男/女):");
students[i][1] = scanner.next();
System.out.print("请输入第" + (i + 1) + "个学生的语文成绩:");
students[i][2] = scanner.next();
System.out.print("请输入第" + (i + 1) + "个学生的数学成绩:");
students[i][3] = scanner.next();
scores[i] = Double.parseDouble(students[i][2]) + Double.parseDouble(students[i][3]);
sum += scores[i];
if (students[i][1].equals("男")) {
maleCount++;
} else {
femaleCount++;
}
}
double average = sum / n;
System.out.println("学生各科成绩信息如下:");
for (int i = 0; i < n; i++) {
System.out.println(students[i][0] + "的语文成绩为:" + students[i][2] + ",数学成绩为:" + students[i][3]);
}
System.out.println("学生平均成绩为:" + average);
System.out.println("学生总成绩为:" + sum);
System.out.println("班级男女比例为:" + maleCount + ":" + femaleCount);
}
}
```
阅读全文