j ava学生管理系统
时间: 2023-11-10 09:52:13 浏览: 56
Java 学生管理系统是一个常见的课程设计项目,它主要用于管理学生信息,包括学生的基本信息、课程信息、成绩信息等。以下是一个简单的 Java 学生管理系统的实现:
1. 创建一个 Student 类,用于存储学生的基本信息,包括学号、姓名、性别、年龄等。
```java
public class Student {
private int id; // 学号
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
public Student(int id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
// getter 和 setter 方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
2. 创建一个 Course 类,用于存储课程信息,包括课程编号、课程名称、学分等。
```java
public class Course {
private int courseId; // 课程编号
private String courseName; // 课程名称
private int credit; // 学分
public Course(int courseId, String courseName, int credit) {
this.courseId = courseId;
this.courseName = courseName;
this.credit = credit;
}
// getter 和 setter 方法
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getCredit() {
return credit;
}
public void setCredit(int credit) {
this.credit = credit;
}
}
```
3. 创建一个 Score 类,用于存储学生成绩信息,包括学生编号、课程编号、分数等。
```java
public class Score {
private int studentId; // 学生编号
private int courseId; // 课程编号
private int score; // 分数
public Score(int studentId, int courseId, int score) {
this.studentId = studentId;
this.courseId = courseId;
this.score = score;
}
// getter 和 setter 方法
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
```
4. 创建一个 StudentManagementSystem 类,用于实现学生管理系统的功能,包括添加学生、删除学生、修改学生信息、查询学生信息等。
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
// 学生列表
private List<Student> students = new ArrayList<>();
// 课程列表
private List<Course> courses = new ArrayList<>();
// 成绩列表
private List<Score> scores = new ArrayList<>();
// 添加学生
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
int id = scanner.nextInt();
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入性别:");
String gender = scanner.next();
System.out.println("请输入年龄:");
int age = scanner.nextInt();
Student student = new Student(id, name, gender, age);
students.add(student);
System.out.println("添加学生信息成功!");
}
// 删除学生
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
int id = scanner.nextInt();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getId() == id) {
students.remove(i);
System.out.println("删除学生信息成功!");
break;
}
}
}
// 修改学生信息
public void updateStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
int id = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入性别:");
String gender = scanner.next();
System.out.println("请输入年龄:");
int age = scanner.nextInt();
student.setName(name);
student.setGender(gender);
student.setAge(age);
System.out.println("修改学生信息成功!");
break;
}
}
}
// 查询学生信息
public void queryStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学号:");
int id = scanner.nextInt();
for (Student student : students) {
if (student.getId() == id) {
System.out.println("学号:" + student.getId());
System.out.println("姓名:" + student.getName());
System.out.println("性别:" + student.getGender());
System.out.println("年龄:" + student.getAge());
break;
}
}
}
// 添加课程
public void addCourse() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入课程编号:");
int courseId = scanner.nextInt();
System.out.println("请输入课程名称:");
String courseName = scanner.next();
System.out.println("请输入学分:");
int credit = scanner.nextInt();
Course course = new Course(courseId, courseName, credit);
courses.add(course);
System.out.println("添加课程信息成功!");
}
// 删除课程
public void deleteCourse() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入课程编号:");
int courseId = scanner.nextInt();
for (int i = 0; i < courses.size(); i++) {
Course course = courses.get(i);
if (course.getCourseId() == courseId) {
courses.remove(i);
System.out.println("删除课程信息成功!");
break;
}
}
}
// 修改课程信息
public void updateCourse() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入课程编号:");
int courseId = scanner.nextInt();
for (Course course : courses) {
if (course.getCourseId() == courseId) {
System.out.println("请输入课程名称:");
String courseName = scanner.next();
System.out.println("请输入学分:");
int credit = scanner.nextInt();
course.setCourseName(courseName);
course.setCredit(credit);
System.out.println("修改课程信息成功!");
break;
}
}
}
// 查询课程信息
public void queryCourse() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入课程编号:");
int courseId = scanner.nextInt();
for (Course course : courses) {
if (course.getCourseId() == courseId) {
System.out.println("课程编号:" + course.getCourseId());
System.out.println("课程名称:" + course.getCourseName());
System.out.println("学分:" + course.getCredit());
break;
}
}
}
// 添加成绩
public void addScore() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生编号:");
int studentId = scanner.nextInt();
System.out.println("请输入课程编号:");
int courseId = scanner.nextInt();
System.out.println("请输入分数:");
int score = scanner.nextInt();
Score s = new Score(studentId, courseId, score);
scores.add(s);
System.out.println("添加成绩信息成功!");
}
// 删除成绩
public void deleteScore() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生编号:");
int studentId = scanner.nextInt();
System.out.println("请输入课程编号:");
int courseId = scanner.nextInt();
for (int i = 0; i < scores.size(); i++) {
Score s = scores.get(i);
if (s.getStudentId() == studentId && s.getCourseId() == courseId) {
scores.remove(i);
System.out.println("删除成绩信息成功!");
break;
}
}
}
// 修改成绩信息
public void updateScore() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生编号:");
int studentId = scanner.nextInt();
System.out.println("请输入课程编号:");
int courseId = scanner.nextInt();
for (Score s : scores) {
if (s.getStudentId() == studentId && s.getCourseId() == courseId) {
System.out.println("请输入分数:");
int score = scanner.nextInt();
s.setScore(score);
System.out.println("修改成绩信息成功!");
break;
}
}
}
// 查询成绩信息
public void queryScore() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生编号:");
int studentId = scanner.nextInt();
System.out.println("请输入课程编号:");
int courseId = scanner.nextInt();
for (Score s : scores) {
if (s.getStudentId() == studentId && s.getCourseId() == courseId) {
System.out.println("学生编号:" + s.getStudentId());
System.out.println("课程编号:" + s.getCourseId());
System.out.println("分数:" + s.getScore());
break;
}
}
}
// 主程序
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManagementSystem system = new StudentManagementSystem();
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. 添加课程信息");
System.out.println("6. 删除课程信息");
System.out.println("7. 修改课程信息");
System.out.println("8. 查询课程信息");
System.out.println("9. 添加成绩信息");
System.out.println("10. 删除成绩信息");
System.out.println("11. 修改成绩信息");
System.out.println("12. 查询成绩信息");
System.out.println("0. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
system.addStudent();
break;
case 2:
system.deleteStudent();
break;
case 3:
system.updateStudent();
break;
case 4:
system.queryStudent();
break;
case 5:
system.addCourse();
break;
case 6:
system.deleteCourse();
break;
case 7:
system.updateCourse();
break;
case 8:
system.queryCourse();
break;
case 9:
system.addScore();
break;
case 10:
system.deleteScore();
break;
case 11:
system.updateScore();
break;
case 12:
system.queryScore();
break;
case 0:
System.exit(0);
default:
System.out.println("无效操作,请重新输入!");
break;
}
}
}
}
```
以上就是一个简单的 Java 学生管理系统的实现。在实际应用中,还可以根据需要对其进行扩展和优化。
阅读全文