import java.util.ArrayList; import java.util.Scanner; class Student { private String name; private int age; private String id; public Student(String name, int age, String id) { this.name = name; this.age = age; 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; } public String getId() { return id; } public void setId(String id) { this.id = id; } @Override public String toString() { return "Student{" + "name='" + name + ''' + ", age=" + age + ", id='" + id + ''' + '}'; } } public class StudentManagementSystem { private static ArrayList<Student> students = new ArrayList<>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择操作:1.添加学生 2.删除学生 3.修改学生 4.查询学生 5.退出"); int choice = scanner.nextInt(); switch (choice) { case 1: addStudent(scanner); break; case 2: deleteStudent(scanner); break; case 3: updateStudent(scanner); break; case 4: queryStudent(scanner); break; case 5: System.out.println("退出系统"); scanner.close(); return; default: System.out.println("无效的选择,请重新输入"); } } } private static void addStudent(Scanner scanner) { System.out.println("请输入学生姓名:"); String name = scanner.next(); System.out.println("请输入学生年龄:"); int age = scanner.nextInt(); System.out.println("请输入学生学号:"); String id = scanner.next(); students.add(new Student(name, age, id)); System.out.println("学生添加成功"); } private static void deleteStudent(Scanner scanner) { System.out.println("请输入要删除的学生学号:"); String id = scanner.next(); for (Student student : students) { if (student.getId().equals(id)) { students.remove(student); System.out.println("学生删除成功"); return; } } System.out.println("未找到该学生"); } private static void updateStudent(Scanner scanner) { System.out.println("请输入要修改的学生学号:"); String id = scanner.next(); for (Student student : students) { if (student.getId().equals(id)) { System.out.println("请输入新的学生姓名:"); String name = scanner.next(); System.out.println("请输入新的学生年龄:"); int age = scanner.nextInt(); student.setName(name); student.setAge(age); System.out.println("学生信息更新成功"); return; } } System.out.println("未找到该学生"); } private static void queryStudent(Scanner scanner) { System.out.println("请输入要查询的学生学号:"); String id = scanner.next(); for (Student student : students) { if (student.getId().equals(id)) { System.out.println(student); return; } } System.out.println("未找到该学生"); } }
这个示例中,我们定义了一个Student
类来表示学生,包含了姓名、年龄和学号等属性,在StudentManagementSystem
类中,我们使用一个ArrayList
来存储学生对象,并提供了添加、删除、修改和查询学生的方法,在main
方法中,我们使用一个循环来让用户选择操作,并根据用户的选择调用相应的方法。
以上就是关于“java 学生管理系统 源码”的问题,朋友们可以点击主页了解更多内容,希望可以够帮助大家!
原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/1184330.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复