先实现学生管理系统的增删改查功能,使用Student类封装学生信息,通过ArrayList存储对象,Scanner接收输入,循环菜单驱动操作,包含添加、查看、查询、修改、删除学生及退出系统,逐步构建基础控制台应用。
想用Java做一个学生管理系统,其实并不难。关键是从简单入手,把功能拆解清楚,一步步实现。下面带你从零开始,写一个基础但完整的控制台版学生管理系统。
作为一个初学者项目,先实现最核心的几个功能:
先创建一个Student类,用来表示学生的基本信息。
public class Student {
private String id;
private String name;
private int age;
public Student() {}
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
// Getter 和 Setter 方法
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;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",年龄:" + age;
}}
使用ArrayList存储学生对象,Sca
nner接收用户输入,通过循环展示菜单并处理选择。
import java.util.ArrayList; import java.util.Scanner;public class StudentManager { public static void main(String[] args) { ArrayList
list = new ArrayList<>(); Scanner sc = new Scanner(System.in); while (true) { System.out.println("\n--- 学生管理系统 ---"); 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.print("请选择操作:"); int choice = sc.nextInt(); sc.nextLine(); // 消费换行符 switch (choice) { case 1: addStudent(list, sc); break; case 2: showStudents(list); break; case 3: findStudent(list, sc); break; case 4: updateStudent(list, sc); break; case 5: deleteStudent(list, sc); break; case 6: System.out.println("再见!"); sc.close(); return; default: System.out.println("输入无效,请重新选择。"); } } } public static void addStudent(ArrayListzuojiankuohaophpcnStudentyoujiankuohaophpcn list, Scanner sc) { System.out.print("请输入学号:"); String id = sc.nextLine(); if (findIndexById(list, id) != -1) { System.out.println("该学号已存在!"); return; } System.out.print("请输入姓名:"); String name = sc.nextLine(); System.out.print("请输入年龄:"); int age = sc.nextInt(); sc.nextLine(); Student s = new Student(id, name, age); list.add(s); System.out.println("添加成功!"); } public static void showStudents(ArrayListzuojiankuohaophpcnStudentyoujiankuohaophpcn list) { if (list.isEmpty()) { System.out.println("暂无学生信息。"); return; } for (Student s : list) { System.out.println(s); } } public static void findStudent(ArrayListzuojiankuohaophpcnStudentyoujiankuohaophpcn list, Scanner sc) { System.out.print("请输入要查询的学号:"); String id = sc.nextLine(); int index = findIndexById(list, id); if (index == -1) { System.out.println("未找到该学生。"); } else { System.out.println("找到学生:" + list.get(index)); } } public static void updateStudent(ArrayListzuojiankuohaophpcnStudentyoujiankuohaophpcn list, Scanner sc) { System.out.print("请输入要修改的学生学号:"); String id = sc.nextLine(); int index = findIndexById(list, id); if (index == -1) { System.out.println("未找到该学生。"); return; } System.out.print("请输入新姓名:"); String name = sc.nextLine(); System.out.print("请输入新年龄:"); int age = sc.nextInt(); sc.nextLine(); Student s = list.get(index); s.setName(name); s.setAge(age); System.out.println("修改成功!"); } public static void deleteStudent(ArrayListzuojiankuohaophpcnStudentyoujiankuohaophpcn list, Scanner sc) { System.out.print("请输入要删除的学生学号:"); String id = sc.nextLine(); int index = findIndexById(list, id); if (index == -1) { System.out.println("未找到该学生。"); return; } list.remove(index); System.out.println("删除成功!"); } // 根据学号查找索引 public static int findIndexById(ArrayListzuojiankuohaophpcnStudentyoujiankuohaophpcn list, String id) { for (int i = 0; i zuojiankuohaophpcn list.size(); i++) { if (list.get(i).getId().equals(id)) { return i; } } return -1; }}
把这个代码复制到你的IDE中(如IntelliJ IDEA或Eclipse),确保两个类在同一个包里。运行StudentManager类,就可以看到菜单并进行操作了。
基本上就这些。这个系统虽然简单,但涵盖了面向对象、集合、流程控制等Java基础知识点,适合练手。之后你可以考虑加入文件读写保存数据,或者改成图形界面。