.任务描述 编写一个程序,模拟学生管理系统。可以实现对学生信息的添加、删除、修改和查询功能,要求使用List集合存储自定义的对象集合,并熟练使用List集合的中常用方法实现相关的操作。
时间: 2024-02-23 17:01:46 浏览: 88
好的,这是一个比较完整的项目,需要一定的编程能力。以下是一个简单的示例代码,仅供参考:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
// 定义一个静态List集合,存储学生信息
private static List<Student> studentList = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 无限循环,直到用户选择退出程序
while (true) {
System.out.println("1. 添加学生信息");
System.out.println("2. 删除学生信息");
System.out.println("3. 修改学生信息");
System.out.println("4. 查询学生信息");
System.out.println("0. 退出程序");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudentInfo(scanner);
break;
case 2:
deleteStudentInfo(scanner);
break;
case 3:
modifyStudentInfo(scanner);
break;
case 4:
queryStudentInfo(scanner);
break;
case 0:
System.out.println("程序已退出。");
System.exit(0);
default:
System.out.println("输入有误,请重新选择。");
break;
}
}
}
// 添加学生信息
private static void addStudentInfo(Scanner scanner) {
System.out.print("请输入学生姓名:");
String name = scanner.next();
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
System.out.print("请输入学生性别(男/女):");
String gender = scanner.next();
Student student = new Student(name, age, gender);
studentList.add(student);
System.out.println("添加成功!");
}
// 删除学生信息
private static void deleteStudentInfo(Scanner scanner) {
System.out.print("请输入要删除的学生姓名:");
String name = scanner.next();
boolean isDeleted = false;
for (Student student : studentList) {
if (name.equals(student.getName())) {
studentList.remove(student);
isDeleted = true;
System.out.println("删除成功!");
break;
}
}
if (!isDeleted) {
System.out.println("未找到该学生的信息。");
}
}
// 修改学生信息
private static void modifyStudentInfo(Scanner scanner) {
System.out.print("请输入要修改的学生姓名:");
String name = scanner.next();
boolean isModified = false;
for (Student student : studentList) {
if (name.equals(student.getName())) {
System.out.print("请输入修改后的学生姓名:");
String newName = scanner.next();
System.out.print("请输入修改后的学生年龄:");
int newAge = scanner.nextInt();
System.out.print("请输入修改后的学生性别(男/女):");
String newGender = scanner.next();
student.setName(newName);
student.setAge(newAge);
student.setGender(newGender);
isModified = true;
System.out.println("修改成功!");
break;
}
}
if (!isModified) {
System.out.println("未找到该学生的信息。");
}
}
// 查询学生信息
private static void queryStudentInfo(Scanner scanner) {
System.out.print("请输入要查询的学生姓名:");
String name = scanner.next();
boolean isFound = false;
for (Student student : studentList) {
if (name.equals(student.getName())) {
isFound = true;
System.out.println(student);
break;
}
}
if (!isFound) {
System.out.println("未找到该学生的信息。");
}
}
}
class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "姓名:" + name + ",年龄:" + age + ",性别:" + gender;
}
}
```
注意,以上代码仅供参考,实际项目中可能需要进行更多的优化和改进。
阅读全文