用java写一个学生管理系统
时间: 2023-12-15 19:04:08 浏览: 61
Java写一个简单学生管理系统
好的,下面是一个简单的学生管理系统的Java代码,包括添加学生、删除学生、修改学生信息和查找学生信息等功能:
```
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> students;
public StudentManagementSystem() {
students = new ArrayList<Student>();
}
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生学号:");
String id = scanner.next();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生性别(男/女):");
String gender = scanner.next();
Student student = new Student(name, id, age, gender);
students.add(student);
System.out.println("添加成功!");
}
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
students.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("没找到该学生!");
}
public void updateStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
System.out.println("请输入学生姓名(原姓名:" + student.getName() + "):");
String name = scanner.next();
System.out.println("请输入学生年龄(原年龄:" + student.getAge() + "):");
int age = scanner.nextInt();
System.out.println("请输入学生性别(原性别:" + student.getGender() + "):");
String gender = scanner.next();
student.setName(name);
student.setAge(age);
student.setGender(gender);
System.out.println("修改成功!");
return;
}
}
System.out.println("没找到该学生!");
}
public void queryStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的学生学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
System.out.println("学生姓名:" + student.getName());
System.out.println("学生学号:" + student.getId());
System.out.println("学生年龄:" + student.getAge());
System.out.println("学生性别:" + student.getGender());
return;
}
}
System.out.println("没找到该学生!");
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManagementSystem system = 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.println("5. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
system.addStudent();
break;
case 2:
system.deleteStudent();
break;
case 3:
system.updateStudent();
break;
case 4:
system.queryStudent();
break;
case 5:
System.exit(0);
}
}
}
}
class Student {
private String name;
private String id;
private int age;
private String gender;
public Student(String name, String id, int age, String gender) {
this.name = name;
this.id = id;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
}
```
以上代码实现了一个简单的学生管理系统,可以添加、删除、修改和查询学生信息。
阅读全文