编写一个程序,模拟学生管理系统Java
时间: 2023-06-26 15:07:58 浏览: 96
模拟学生管理系统程序
以下是一个简单的学生管理系统Java程序,实现了添加、查询和删除学生信息的功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
static ArrayList<Student> studentList = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
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();
switch (option) {
case 1:
addStudent();
break;
case 2:
queryStudent();
break;
case 3:
deleteStudent();
break;
case 4:
System.out.println("退出程序");
System.exit(0);
default:
System.out.println("无效选项");
}
}
}
public static void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生姓名:");
String name = scanner.next();
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
System.out.print("请输入学生性别:");
String gender = scanner.next();
System.out.print("请输入学生班级:");
String clazz = scanner.next();
Student student = new Student(name, age, gender, clazz);
studentList.add(student);
System.out.println("添加成功!");
}
public static void queryStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生姓名或学号:");
String keyword = scanner.next();
for (Student student : studentList) {
if (student.getName().equals(keyword) || student.getId().equals(keyword)) {
System.out.println(student);
return;
}
}
System.out.println("未找到匹配记录");
}
public static void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生姓名或学号:");
String keyword = scanner.next();
for (Student student : studentList) {
if (student.getName().equals(keyword) || student.getId().equals(keyword)) {
studentList.remove(student);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到匹配记录");
}
}
class Student {
private static int idCounter = 1;
private String id;
private String name;
private int age;
private String gender;
private String clazz;
public Student(String name, int age, String gender, String clazz) {
this.id = "S" + String.format("%03d", idCounter++);
this.name = name;
this.age = age;
this.gender = gender;
this.clazz = clazz;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getClazz() {
return clazz;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",年龄:" + age + ",性别:" + gender + ",班级:" + clazz;
}
}
```
程序使用了ArrayList来存储学生信息,通过循环遍历实现了查询和删除功能。学生类使用了静态变量idCounter来实现自增的学号。
阅读全文