Java编写一个学生管理系统
时间: 2023-10-16 11:11:03 浏览: 69
java编写的学生管理系统
这里提供一个简单的学生管理系统的Java代码,实现了添加学生、删除学生、查找学生和打印学生列表等基本功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> students = new ArrayList<>();
public void start() {
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.println("0. 退出");
int choice = scanner.nextInt();
if (choice == 0) {
break;
}
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
findStudent();
break;
case 4:
printStudents();
break;
default:
System.out.println("请输入正确的操作编号!");
break;
}
}
}
private void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生性别(男/女):");
String gender = scanner.next();
System.out.println("请输入学生班级:");
String clazz = scanner.next();
Student student = new Student(name, age, gender, clazz);
students.add(student);
System.out.println("添加成功!");
}
private void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生姓名:");
String name = scanner.nextLine();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getName().equals(name)) {
students.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到要删除的学生!");
}
private void findStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查找的学生姓名:");
String name = scanner.nextLine();
for (Student student : students) {
if (student.getName().equals(name)) {
System.out.println(student);
return;
}
}
System.out.println("没有找到要查找的学生!");
}
private void printStudents() {
if (students.size() == 0) {
System.out.println("当前没有学生信息!");
} else {
for (Student student : students) {
System.out.println(student);
}
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.start();
}
}
class Student {
private String name;
private int age;
private String gender;
private String clazz;
public Student(String name, int age, String gender, String clazz) {
this.name = name;
this.age = age;
this.gender = gender;
this.clazz = clazz;
}
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 "学生姓名:" + name + ",年龄:" + age + ",性别:" + gender + ",班级:" + clazz;
}
}
```
使用时,可以直接运行 `StudentManagementSystem` 类即可。
阅读全文