用Java生成一个学生管理系统
时间: 2023-12-19 07:06:23 浏览: 74
好的,下面我将提供一个简单的学生管理系统的Java代码示例,其中包括学生信息的添加、查询、修改和删除功能。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> studentList;
public StudentManagementSystem() {
studentList = new ArrayList<Student>();
}
public void addStudent() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生的姓名:");
String name = sc.nextLine();
System.out.println("请输入学生的年龄:");
int age = sc.nextInt();
System.out.println("请输入学生的性别(男/女):");
String gender = sc.next();
System.out.println("请输入学生的班级:");
String className = sc.next();
Student student = new Student(name, age, gender, className);
studentList.add(student);
System.out.println("学生信息添加成功!");
}
public void queryStudent() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查询的学生姓名:");
String name = sc.nextLine();
for (Student student : studentList) {
if (name.equals(student.getName())) {
System.out.println(student.toString());
return;
}
}
System.out.println("未找到该学生信息!");
}
public void updateStudent() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的学生姓名:");
String name = sc.nextLine();
for (Student student : studentList) {
if (name.equals(student.getName())) {
System.out.println("请输入学生的年龄:");
int age = sc.nextInt();
System.out.println("请输入学生的性别(男/女):");
String gender = sc.next();
System.out.println("请输入学生的班级:");
String className = sc.next();
student.setAge(age);
student.setGender(gender);
student.setClassName(className);
System.out.println("学生信息修改成功!");
return;
}
}
System.out.println("未找到该学生信息!");
}
public void deleteStudent() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学生姓名:");
String name = sc.nextLine();
for (Student student : studentList) {
if (name.equals(student.getName())) {
studentList.remove(student);
System.out.println("学生信息删除成功!");
return;
}
}
System.out.println("未找到该学生信息!");
}
public void displayAllStudents() {
for (Student student : studentList) {
System.out.println(student.toString());
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StudentManagementSystem sms = 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. 退出");
int choice = sc.nextInt();
switch (choice) {
case 1:
sms.addStudent();
break;
case 2:
sms.queryStudent();
break;
case 3:
sms.updateStudent();
break;
case 4:
sms.deleteStudent();
break;
case 5:
sms.displayAllStudents();
break;
case 6:
System.out.println("程序已退出!");
System.exit(0);
default:
System.out.println("无效的操作,请重新选择!");
break;
}
}
}
}
class Student {
private String name;
private int age;
private String gender;
private String className;
public Student(String name, int age, String gender, String className) {
this.name = name;
this.age = age;
this.gender = gender;
this.className = className;
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String toString() {
return "姓名:" + name + "\t年龄:" + age + "\t性别:" + gender + "\t班级:" + className;
}
}
```
该代码中首先创建了一个 `StudentManagementSystem` 类,其中包含了学生信息的添加、查询、修改和删除方法,以及显示所有学生信息的方法。在 `main` 方法中,使用一个无限循环来控制程序的执行,用户可以根据菜单选择要执行的操作。
同时,还创建了一个 `Student` 类,用来表示学生对象,并提供了相应的属性和方法。
该学生管理系统只是一个简单的示例,可以根据实际需求进行适当的修改和扩展。
阅读全文