开始尝试写最基础的学生管理系统
import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Student { private String name; private int age; private String grade; public Student(String name, int age, String grade) { this.name = name; this.age = age; this.grade = grade; } public String getName() { return name; } public int getAge() { return age; } public String getGrade() { return grade; } } class StudentManagementSystem { private List<Student> students; public StudentManagementSystem() { students = new ArrayList<>(); } public void addStudent(Student student) { students.add(student); } public void removeStudent(String name) { students.removeIf(s -> s.getName().equals(name)); } public List<Student> getStudents() { return students; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); StudentManagementSystem sms = new StudentManagementSystem(); while (true) { System.out.println("学生管理系统"); System.out.println("1. 添加学生"); System.out.println("2. 删除学生"); System.out.println("3. 显示所有学生"); System.out.println("4. 退出系统"); System.out.print("请输入选项: "); int option = scanner.nextInt(); scanner.nextLine(); if (option == 1) { System.out.print("请输入学生姓名: "); String name = scanner.nextLine(); System.out.print("请输入学生年龄: "); int age = scanner.nextInt(); scanner.nextLine(); System.out.print("请输入学生年级: "); String grade = scanner.nextLine(); Student student = new Student(name, age, grade); sms.addStudent(student); System.out.println("添加学生成功!"); } else if (option == 2) { System.out.print("请输入要删除的学生姓名: "); String name = scanner.nextLine(); sms.removeStudent(name); System.out.println("删除学生成功!"); } else if (option == 3) { List<Student> students = sms.getStudents(); if (students.isEmpty()) { System.out.println("暂无学生信息!"); } else { System.out.println("学生信息如下:"); for (Student student : students) { System.out.println("姓名: " + student.getName()); System.out.println("年龄: " + student.getAge()); System.out.println("年级: " + student.getGrade()); System.out.println("------------------------"); } } } else if (option == 4) { System.out.println("退出学生管理系统。"); break; } else { System.out.println("无效的选项,请重新输入!"); } System.out.println(); } scanner.close(); } }
标签:七月,String,第三周,System,name,九日,println,public,out From: https://www.cnblogs.com/youxiandechilun/p/17556859.html