学生成绩管理 功能要求:按照以下要求构造出学生类Student,设计和编写相应的成员变量,构造函数以及成员方法,并编写StudentScore类来完成以下的功能: 编写控制台主界面,根据提示,输入数字:1代表显示所有学生信息,2代表计算当前每个学生的平均成绩,3代表退出程序。Java
时间: 2024-03-23 12:42:07 浏览: 54
好的,我理解了你的问题。以下是我提供的实现代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Student {
private String name;
private int id;
private int mathScore;
private int englishScore;
private int computerScore;
public Student(String name, int id, int mathScore, int englishScore, int computerScore) {
this.name = name;
this.id = id;
this.mathScore = mathScore;
this.englishScore = englishScore;
this.computerScore = computerScore;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int getMathScore() {
return mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public int getComputerScore() {
return computerScore;
}
}
public class StudentScore {
private ArrayList<Student> students;
public StudentScore() {
students = new ArrayList<Student>();
}
public void addStudent(Student student) {
students.add(student);
}
public void showAllStudents() {
System.out.println("所有学生信息如下:");
for (Student student : students) {
System.out.println("姓名:" + student.getName() + ",学号:" + student.getId() + ",数学成绩:" + student.getMathScore() + ",英语成绩:" + student.getEnglishScore() + ",计算机成绩:" + student.getComputerScore());
}
}
public void calculateAverageScore() {
for (Student student : students) {
int sumScore = student.getMathScore() + student.getEnglishScore() + student.getComputerScore();
double averageScore = (double) sumScore / 3;
System.out.println(student.getName() + "的平均成绩为:" + averageScore);
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentScore studentScore = new StudentScore();
// 添加学生信息
studentScore.addStudent(new Student("张三", 1001, 75, 80, 90));
studentScore.addStudent(new Student("李四", 1002, 85, 90, 95));
studentScore.addStudent(new Student("王五", 1003, 90, 85, 80));
// 控制台主界面
while (true) {
System.out.println("请输入数字选择操作:1.显示所有学生信息 2.计算每个学生的平均成绩 3.退出程序");
int choice = scanner.nextInt();
switch (choice) {
case 1:
studentScore.showAllStudents();
break;
case 2:
studentScore.calculateAverageScore();
break;
case 3:
System.out.println("程序已退出");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入");
}
}
}
}
```
以上代码中,我们定义了一个 `Student` 类来表示学生信息,包括姓名、学号、数学成绩、英语成绩、计算机成绩等属性。然后,我们又定义了一个 `StudentScore` 类来管理学生成绩,包括添加学生信息、显示所有学生信息、计算每个学生的平均成绩等方法。最后,我们在 `Main` 类中实现了控制台主界面,用户可以根据提示输入数字选择操作。
阅读全文