用java编写一个学生成绩管理系统。学生的属性包括学号、姓名、年龄等。每个学生要学习若干课程,每门课程有平时成绩、其中考试成绩、实习成绩、期末考试成绩以及总评成绩等多个成绩,其中平时成绩的数目不定
时间: 2023-12-22 21:57:51 浏览: 100
基于Java的学生成绩管理系统设计源码
以下是一个简单的学生成绩管理系统的Java代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentGradeSystem {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<Student>();
while (true) {
System.out.println("请输入操作编号:");
System.out.println("1. 添加学生");
System.out.println("2. 删除学生");
System.out.println("3. 修改学生信息");
System.out.println("4. 查询学生信息");
System.out.println("5. 退出系统");
int choice = sc.nextInt();
switch (choice) {
case 1:
addStudent(students, sc);
break;
case 2:
deleteStudent(students, sc);
break;
case 3:
modifyStudent(students, sc);
break;
case 4:
queryStudent(students, sc);
break;
case 5:
System.out.println("谢谢使用!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
// 添加学生
public static void addStudent(ArrayList<Student> students, Scanner sc) {
System.out.println("请输入学号:");
String id = sc.next();
System.out.println("请输入姓名:");
String name = sc.next();
System.out.println("请输入年龄:");
int age = sc.nextInt();
Student student = new Student(id, name, age);
System.out.println("请输入该学生选修课程的数量:");
int courseNum = sc.nextInt();
for (int i = 0; i < courseNum; i++) {
System.out.println("请输入第" + (i+1) + "门课程的名称:");
String courseName = sc.next();
System.out.println("请输入该课程的平时成绩数目:");
int dailyScoreNum = sc.nextInt();
Course course = new Course(courseName);
for (int j = 0; j < dailyScoreNum; j++) {
System.out.println("请输入该课程的第" + (j+1) + "个平时成绩:");
double dailyScore = sc.nextDouble();
course.addDailyScore(dailyScore);
}
System.out.println("请输入该课程的期中考试成绩:");
double midtermScore = sc.nextDouble();
course.setMidtermScore(midtermScore);
System.out.println("请输入该课程的实习成绩:");
double internshipScore = sc.nextDouble();
course.setInternshipScore(internshipScore);
System.out.println("请输入该课程的期末考试成绩:");
double finalScore = sc.nextDouble();
course.setFinalScore(finalScore);
student.addCourse(course);
}
students.add(student);
System.out.println("添加成功!");
}
// 删除学生
public static void deleteStudent(ArrayList<Student> students, Scanner sc) {
System.out.println("请输入要删除的学生的学号:");
String id = sc.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("该学号对应的学生不存在!");
}
// 修改学生信息
public static void modifyStudent(ArrayList<Student> students, Scanner sc) {
System.out.println("请输入要修改的学生的学号:");
String id = sc.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
System.out.println("请输入新的姓名:");
String name = sc.next();
System.out.println("请输入新的年龄:");
int age = sc.nextInt();
students.get(i).setName(name);
students.get(i).setAge(age);
System.out.println("修改成功!");
return;
}
}
System.out.println("该学号对应的学生不存在!");
}
// 查询学生信息
public static void queryStudent(ArrayList<Student> students, Scanner sc) {
System.out.println("请输入要查询的学生的学号:");
String id = sc.next();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
System.out.println("学号:" + students.get(i).getId());
System.out.println("姓名:" + students.get(i).getName());
System.out.println("年龄:" + students.get(i).getAge());
System.out.println("选修课程:");
ArrayList<Course> courses = students.get(i).getCourses();
for (int j = 0; j < courses.size(); j++) {
System.out.println("课程名称:" + courses.get(j).getName());
System.out.println("平时成绩:");
ArrayList<Double> dailyScores = courses.get(j).getDailyScores();
for (int k = 0; k < dailyScores.size(); k++) {
System.out.println("第" + (k+1) + "个平时成绩:" + dailyScores.get(k));
}
System.out.println("期中考试成绩:" + courses.get(j).getMidtermScore());
System.out.println("实习成绩:" + courses.get(j).getInternshipScore());
System.out.println("期末考试成绩:" + courses.get(j).getFinalScore());
System.out.println("总评成绩:" + courses.get(j).getTotalScore());
}
return;
}
}
System.out.println("该学号对应的学生不存在!");
}
}
class Student {
private String id;
private String name;
private int age;
private ArrayList<Course> courses;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
this.courses = new ArrayList<Course>();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public ArrayList<Course> getCourses() {
return courses;
}
public void addCourse(Course course) {
courses.add(course);
}
public void removeCourse(Course course) {
courses.remove(course);
}
}
class Course {
private String name;
private ArrayList<Double> dailyScores;
private double midtermScore;
private double internshipScore;
private double finalScore;
private double totalScore;
public Course(String name) {
this.name = name;
this.dailyScores = new ArrayList<Double>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Double> getDailyScores() {
return dailyScores;
}
public void addDailyScore(double score) {
dailyScores.add(score);
}
public double getMidtermScore() {
return midtermScore;
}
public void setMidtermScore(double midtermScore) {
this.midtermScore = midtermScore;
}
public double getInternshipScore() {
return internshipScore;
}
public void setInternshipScore(double internshipScore) {
this.internshipScore = internshipScore;
}
public double getFinalScore() {
return finalScore;
}
public void setFinalScore(double finalScore) {
this.finalScore = finalScore;
}
public double getTotalScore() {
totalScore = 0;
for (int i = 0; i < dailyScores.size(); i++) {
totalScore += dailyScores.get(i);
}
totalScore += midtermScore + internshipScore + finalScore;
return totalScore;
}
}
```
这个例子中,我们定义了两个类,`Student` 和 `Course`,用于表示学生和课程。`Student` 类包含一个学生的基本信息(学号、姓名、年龄)和选修课程(用一个 `ArrayList` 存储)。`Course` 类包含一个课程的基本信息(名称)和各项成绩(平时成绩、期中考试成绩、实习成绩、期末考试成绩、总评成绩)。
在 `main` 函数中,我们使用一个 `ArrayList` 存储所有学生的信息,并通过一个无限循环来实现菜单式的操作。用户可以输入对应的编号来执行不同的操作,包括添加学生、删除学生、修改学生信息和查询学生信息。
在添加学生时,我们先输入学生的基本信息,然后再输入该学生选修课程的数量。对于每门课程,我们输入名称和平时成绩数目,然后分别输入各项成绩,并将其添加到该学生的选修课程中。
在删除学生、修改学生信息和查询学生信息时,我们都需要先输入要操作的学生的学号,然后通过遍历 `ArrayList` 来找到对应的学生,进行相应的操作。
以上是一个基本的学生成绩管理系统的Java代码示例,可以根据需要进行修改和完善。
阅读全文