创建一个对象数组数组的元素是学生对象学生的信息包括学号姓名和成绩求其总人数和平均分学生对象不得少于5个
时间: 2024-04-30 14:18:21 浏览: 73
使用对象数组管理学生成绩
以下是一个示例代码,创建了一个名为`students`的学生对象数组,包含5个学生对象,每个学生对象拥有学号、姓名和成绩三个属性。代码中求出了学生总人数和平均分。
```java
public class Student {
private int id;
private String name;
private int score;
public Student(int id, String name, int score) {
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[5];
students[0] = new Student(1, "Alice", 80);
students[1] = new Student(2, "Bob", 90);
students[2] = new Student(3, "Charlie", 85);
students[3] = new Student(4, "David", 95);
students[4] = new Student(5, "Eve", 75);
int total = students.length;
int sum = 0;
for (int i = 0; i < students.length; i++) {
sum += students[i].getScore();
}
double avg = (double) sum / total;
System.out.println("Total number of students: " + total);
System.out.println("Average score: " + avg);
}
}
```
阅读全文