1、 创建一个对象数组,数组的元素是学生对象,学生的信息包括学号、姓名和成绩(一门课成绩),求其总人数和平均分。(学生对象不得少于5个)
时间: 2024-01-04 09:02:09 浏览: 97
C语言程序设计-有一个一维数组score,内放10个学生的成绩,用一个函数来求平均成绩;
5星 · 资源好评率100%
以下是一个示例代码,创建了5个学生对象,并计算了总人数和平均分:
```java
class Student {
int id;
String name;
double score;
public Student(int id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[5];
students[0] = new Student(1, "张三", 90);
students[1] = new Student(2, "李四", 80);
students[2] = new Student(3, "王五", 70);
students[3] = new Student(4, "赵六", 85);
students[4] = new Student(5, "钱七", 95);
int count = students.length;
double sum = 0;
for (int i = 0; i < count; i++) {
sum += students[i].score;
}
double average = sum / count;
System.out.println("总人数:" + count);
System.out.println("平均分:" + average);
}
}
```
输出结果为:
```
总人数:5
平均分:84.0
```
阅读全文