用java写一个简易的学生学籍管理系统。要有学生信息录入,查询,修改,删除
时间: 2024-03-06 22:49:48 浏览: 123
好的,下面是一个简单的学生学籍管理系统的Java代码,实现了学生信息的录入、查询、修改和删除功能。请注意,这只是一个简单版本的示例代码,您可以根据自己的需求进行修改和完善。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<Student>();
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("请选择操作:");
System.out.println("1. 录入学生信息");
System.out.println("2. 查询学生信息");
System.out.println("3. 修改学生信息");
System.out.println("4. 删除学生信息");
System.out.println("0. 退出");
choice = input.nextInt();
switch (choice) {
case 1:
addStudent(students);
break;
case 2:
searchStudent(students);
break;
case 3:
updateStudent(students);
break;
case 4:
deleteStudent(students);
break;
case 0:
System.out.println("已退出学生学籍管理系统");
break;
default:
System.out.println("输入有误,请重新选择操作!");
break;
}
} while (choice != 0);
input.close();
}
// 添加学生信息
public static void addStudent(ArrayList<Student> students) {
Scanner input = new Scanner(System.in);
System.out.println("请输入学生编号:");
String id = input.nextLine();
System.out.println("请输入学生姓名:");
String name = input.nextLine();
System.out.println("请输入学生年龄:");
int age = input.nextInt();
System.out.println("请输入学生性别(男/女):");
String gender = input.next();
Student student = new Student(id, name, age, gender);
students.add(student);
System.out.println("学生信息已添加!");
}
// 查询学生信息
public static void searchStudent(ArrayList<Student> students) {
Scanner input = new Scanner(System.in);
System.out.println("请输入要查询的学生姓名:");
String name = input.nextLine();
boolean flag = false;
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println("学生信息如下:");
System.out.println(student);
flag = true;
break;
}
}
if (!flag) {
System.out.println("未找到该学生信息!");
}
}
// 修改学生信息
public static void updateStudent(ArrayList<Student> students) {
Scanner input = new Scanner(System.in);
System.out.println("请输入要修改的学生编号:");
String id = input.nextLine();
boolean flag = false;
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println("请输入修改后的学生姓名:");
String name = input.nextLine();
System.out.println("请输入修改后的学生年龄:");
int age = input.nextInt();
System.out.println("请输入修改后的学生性别(男/女):");
String gender = input.next();
student.setName(name);
student.setAge(age);
student.setGender(gender);
System.out.println("学生信息已修改!");
flag = true;
break;
}
}
if (!flag) {
System.out.println("未找到该学生信息!");
}
}
// 删除学生信息
public static void deleteStudent(ArrayList<Student> students) {
Scanner input = new Scanner(System.in);
System.out.println("请输入要删除的学生编号:");
String id = input.nextLine();
boolean flag = false;
for (Student student : students) {
if (student.getId().equals(id)) {
students.remove(student);
System.out.println("学生信息已删除!");
flag = true;
break;
}
}
if (!flag) {
System.out.println("未找到该学生信息!");
}
}
}
class Student {
private String id;
private String name;
private int age;
private String gender;
public Student(String id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
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;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",年龄:" + age + ",性别:" + gender;
}
}
```
以上代码实现了学生信息的录入、查询、修改和删除功能,具体操作请按照提示输入对应的数字,然后按回车键执行相应的操作。
阅读全文