先实现学生管理系统的增删改查功能,使用Student类封装学生信息,通过ArrayList存储对象,Scanner接收输入,循环菜单驱动操作,包含添加、查看、查询、修改、删除学生及退出系统,逐步构建基础控制台应用。

想用Java做一个学生管理系统,其实并不难。关键是从简单入手,把功能拆解清楚,一步步实现。下面带你从零开始,写一个基础但完整的控制台版学生管理系统。
1. 明确系统基本功能
作为一个初学者项目,先实现最核心的几个功能:
- 添加学生:输入学号、姓名、年龄
- 查看所有学生:列出当前所有学生信息
- 根据学号查询学生
- 修改学生信息
- 删除学生
- 退出系统
2. 设计学生类(Student)
先创建一个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;
}}
立即学习“Java免费学习笔记(深入)”;
3. 主程序逻辑(StudentManager)
使用ArrayList存储学生对象,Scanner接收用户输入,通过循环展示菜单并处理选择。
建站之星网站建设系统是一种全新的互联网应用模式,它一改过去传统的企业建站方式,不需企业编写任何程序或网页,无需学习任何相关语言,也不需第三方代写或管理网站,只需应用系统所提供的各种强大丰富的功能模块,即可轻松生成企业个性化的精美网站。 SiteStar v2.3本地软件体验包说明:为方便客户能够第一时间体验智能建站软件的强大功能,我们特别提供了本地软件体验包,您只需下载下来并安装在您的计算机上(和
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; }}
立即学习“Java免费学习笔记(深入)”;
4. 运行与测试建议
把这个代码复制到你的IDE中(如IntelliJ IDEA或Eclipse),确保两个类在同一个包里。运行StudentManager类,就可以看到菜单并进行操作了。
- 每写完一个功能,先测试一下是否正常
- 注意处理重复学号、空列表等边界情况
- 输入时注意nextLine()和nextInt()混用的问题,记得清理缓冲区
基本上就这些。这个系统虽然简单,但涵盖了面向对象、集合、流程控制等Java基础知识点,适合练手。之后你可以考虑加入文件读写保存数据,或者改成图形界面。









