学生管理系统Java代码
时间: 2023-10-11 14:08:48 浏览: 62
学生信息管理系统Java+代码.pdf
以下是一个简单的学生管理系统的Java代码示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private static ArrayList<Student> students = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("1. 添加学生信息");
System.out.println("2. 删除学生信息");
System.out.println("3. 修改学生信息");
System.out.println("4. 查看学生信息");
System.out.println("5. 退出系统");
System.out.println("请选择操作:");
int option = scanner.nextInt();
switch (option) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
modifyStudent();
break;
case 4:
viewStudents();
break;
case 5:
System.exit(0);
default:
System.out.println("无效操作");
break;
}
}
}
private static void addStudent() {
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生性别:");
String gender = scanner.next();
System.out.println("请输入学生班级:");
String clazz = scanner.next();
students.add(new Student(name, age, gender, clazz));
System.out.println("添加成功");
}
private static void deleteStudent() {
System.out.println("请输入要删除的学生姓名:");
String name = scanner.next();
for (Student student : students) {
if (student.getName().equals(name)) {
students.remove(student);
System.out.println("删除成功");
return;
}
}
System.out.println("未找到该学生");
}
private static void modifyStudent() {
System.out.println("请输入要修改的学生姓名:");
String name = scanner.next();
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println("请输入新的学生姓名:");
String newName = scanner.next();
System.out.println("请输入新的学生年龄:");
int newAge = scanner.nextInt();
System.out.println("请输入新的学生性别:");
String newGender = scanner.next();
System.out.println("请输入新的学生班级:");
String newClazz = scanner.next();
student.setName(newName);
student.setAge(newAge);
student.setGender(newGender);
student.setClazz(newClazz);
System.out.println("修改成功");
return;
}
}
System.out.println("未找到该学生");
}
private static void viewStudents() {
System.out.println("学生姓名\t学生年龄\t学生性别\t学生班级");
for (Student student : students) {
System.out.printf("%s\t%d\t%s\t%s\n", student.getName(), student.getAge(), student.getGender(), student.getClazz());
}
}
}
class Student {
private String name;
private int age;
private String gender;
private String clazz;
public Student(String name, int age, String gender, String clazz) {
this.name = name;
this.age = age;
this.gender = gender;
this.clazz = clazz;
}
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 getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
}
```
这个学生管理系统可以实现添加、删除、修改、查看学生信息的功能。当用户选择退出系统时,程序会终止。该程序使用了ArrayList来存储学生信息,用Scanner来获取用户输入。注意,在删除和修改学生信息时,程序需要先查找学生是否存在,如果不存在则提示用户未找到该学生。
阅读全文