先实现学生管理系统的增删改查功能,使用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;}@Overridepublic String toString() { return "学号:" + id + ",姓名:" + name + ",年龄:" + age;}
}
立即学习“Java免费学习笔记(深入)”;
3. 主程序逻辑(StudentManager)
使用ArrayList存储学生对象,Scanner接收用户输入,通过循环展示菜单并处理选择。
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(ArrayList 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(ArrayList list) { if (list.isEmpty()) { System.out.println("暂无学生信息。"); return; } for (Student s : list) { System.out.println(s); }}public static void findStudent(ArrayList 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(ArrayList 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(ArrayList 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(ArrayList list, String id) { for (int i = 0; i < list.size(); i++) { if (list.get(i).getId().equals(id)) { return i; } } return -1;}
}
立即学习“Java免费学习笔记(深入)”;
4. 运行与测试建议
把这个代码复制到你的IDE中(如IntelliJ IDEA或Eclipse),确保两个类在同一个包里。运行StudentManager类,就可以看到菜单并进行操作了。
每写完一个功能,先测试一下是否正常注意处理重复学号、空列表等边界情况输入时注意nextLine()和nextInt()混用的问题,记得清理缓冲区
基本上就这些。这个系统虽然简单,但涵盖了面向对象、集合、流程控制等Java基础知识点,适合练手。之后你可以考虑加入文件读写保存数据,或者改成图形界面。
以上就是初学者如何用Java写一个学生管理系统的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/74594.html
微信扫一扫
支付宝扫一扫