1、创建一个对象数组,数组的元素是学生对象,学生的信息包括学号、姓名和成绩(一门课成绩),求其总人数和平均分。(学生对象不得少于5个)
时间: 2023-06-05 19:47:05 浏览: 286
可以这样创建一个学生对象数组:
```java
class Student {
String id; // 学号
String name; // 姓名
int score; // 成绩
}
Student[] students = new Student[5];
students[] = new Student();
students[].id = "001";
students[].name = "张三";
students[].score = 80;
students[1] = new Student();
students[1].id = "002";
students[1].name = "李四";
students[1].score = 90;
students[2] = new Student();
students[2].id = "003";
students[2].name = "王五";
students[2].score = 70;
students[3] = new Student();
students[3].id = "004";
students[3].name = "赵六";
students[3].score = 85;
students[4] = new Student();
students[4].id = "005";
students[4].name = "钱七";
students[4].score = 95;
```
然后可以用循环遍历数组,计算总人数和总分数,最后求平均分:
```java
int totalScore = ;
int totalStudents = students.length;
for (int i = ; i < students.length; i++) {
totalScore += students[i].score;
}
double averageScore = (double) totalScore / totalStudents;
System.out.println("总人数:" + totalStudents);
System.out.println("平均分:" + averageScore);
```
阅读全文
相关推荐
















