顺序表编写学生成绩管理程序的代码用Java
时间: 2024-09-17 07:07:43 浏览: 41
在Java中,我们可以使用数组或ArrayList(一种动态数组)来实现顺序列表的数据结构,用于存储学生的成绩信息。这里是一个简单的例子,展示如何创建一个顺序表并管理学生成绩:
```java
import java.util.ArrayList;
class Student {
String name;
int score;
// 构造函数、getter和setter省略
public void setScore(int newScore) {
this.score = newScore;
}
}
public class ScoreList {
private ArrayList<Student> students;
public ScoreList() {
students = new ArrayList<>();
}
// 添加新学生
public void addStudent(String name, int score) {
Student student = new Student();
student.name = name;
student.setScore(score);
students.add(student);
}
// 获取学生平均分
public double getAverageScore() {
if (students.isEmpty()) return 0;
int sum = 0;
for (Student s : students) {
sum += s.score;
}
return (double) sum / students.size();
}
// 打印所有学生及成绩
public void printStudents() {
for (Student s : students) {
System.out.println("Name: " + s.name + ", Score: " + s.score);
}
}
public static void main(String[] args) {
ScoreList list = new ScoreList();
list.addStudent("Alice", 90);
list.addStudent("Bob", 85);
list.printStudents(); // 输出学生信息
System.out.println("Average Score: " + list.getAverageScore()); // 输出平均分
}
}
```
阅读全文