java设一个班有10名学生,每个学生有姓名、学号、成绩等信息,请利用对象数组的概念编程,求平均成绩。
时间: 2023-05-18 09:03:22 浏览: 110
编程实现:(1) 键入某组学生(共 10 人)的学号、姓名、组成原理考试 成绩、数据结构考试成绩、汇编语言考试成绩
好的,这是一个简单的 Java 编程问题,我可以回答。以下是一个示例代码:
```java
public class Student {
String name;
int id;
double score;
public Student(String name, int id, double score) {
this.name = name;
this.id = id;
this.score = score;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[10];
students[0] = new Student("Tom", 1, 80);
students[1] = new Student("Jerry", 2, 90);
students[2] = new Student("Alice", 3, 85);
students[3] = new Student("Bob", 4, 95);
students[4] = new Student("David", 5, 70);
students[5] = new Student("Eva", 6, 75);
students[6] = new Student("Frank", 7, 88);
students[7] = new Student("Grace", 8, 92);
students[8] = new Student("Henry", 9, 87);
students[9] = new Student("Ivy", 10, 83);
double sum = 0;
for (int i = 0; i < students.length; i++) {
sum += students[i].score;
}
double average = sum / students.length;
System.out.println("The average score is: " + average);
}
}
```
这个程序定义了一个 `Student` 类,包含了学生的姓名、学号和成绩等信息。然后在 `Main` 类中创建了一个由 10 个学生组成的数组,并计算了这些学生的平均成绩。
阅读全文