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

想用Java做一个学生管理系统,其实并不难。关键是从简单入手,把功能拆解清楚,一步步实现。下面带你从零开始,写一个基础但完整的控制台版学生管理系统。
作为一个初学者项目,先实现最核心的几个功能:
先创建一个Student类,用来表示学生的基本信息。
public class Student {
private String id;
private String name;
private int age;
<pre class='brush:java;toolbar:false;'>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免费学习笔记(深入)”;
使用ArrayList存储学生对象,Scanner接收用户输入,通过循环展示菜单并处理选择。
import java.util.ArrayList;
import java.util.Scanner;
<p>public class StudentManager {
public static void main(String[] args) {
ArrayList<Student> 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("请选择操作:");</p><pre class='brush:java;toolbar:false;'> 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<Student> 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<Student> list) {
if (list.isEmpty()) {
System.out.println("暂无学生信息。");
return;
}
for (Student s : list) {
System.out.println(s);
}
}
public static void findStudent(ArrayList<Student> 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<Student> 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<Student> 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<Student> list, String id) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getId().equals(id)) {
return i;
}
}
return -1;
}}
立即学习“Java免费学习笔记(深入)”;
把这个代码复制到你的IDE中(如IntelliJ IDEA或Eclipse),确保两个类在同一个包里。运行StudentManager类,就可以看到菜单并进行操作了。
基本上就这些。这个系统虽然简单,但涵盖了面向对象、集合、流程控制等Java基础知识点,适合练手。之后你可以考虑加入文件读写保存数据,或者改成图形界面。
以上就是初学者如何用Java写一个学生管理系统的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号