java实现学生管理系统
时间: 2023-08-16 07:09:37 浏览: 79
学生管理系统是一个典型的信息管理系统,可以使用Java语言实现。以下是一个简单的学生管理系统实现的示例代码:
```java
import java.util.Scanner;
public class StudentManagementSystem {
public static void main(String[] args) {
// 创建一个Scanner对象,用于读取用户的输入
Scanner scanner = new Scanner(System.in);
// 定义一个数组,用于存储学生信息
Student[] students = new Student[100];
// 定义一个变量,记录学生数量
int count = 0;
// 循环显示菜单,直到用户选择退出
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. 显示所有学生");
System.out.println("0. 退出系统");
System.out.println("***************************");
// 读取用户的选择
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent(scanner, students, count);
count++;
break;
case 2:
deleteStudent(scanner, students, count);
break;
case 3:
updateStudent(scanner, students, count);
break;
case 4:
queryStudent(scanner, students, count);
break;
case 5:
showAllStudents(students, count);
break;
case 0:
System.out.println("谢谢使用,再见!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
// 输出一个空行,用于分隔不同的操作
System.out.println();
}
}
// 添加学生
public static void addStudent(Scanner scanner, Student[] students, int count) {
System.out.print("请输入学号:");
String id = scanner.next();
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();
students[count] = new Student(id, name, age, gender, clazz);
System.out.println("添加成功!");
}
// 删除学生
public static void deleteStudent(Scanner scanner, Student[] students, int count) {
System.out.print("请输入要删除的学生的学号:");
String id = scanner.next();
int index = findStudentIndexById(id, students, count);
if (index == -1) {
System.out.println("该学生不存在!");
} else {
for (int i = index; i < count - 1; i++) {
students[i] = students[i + 1];
}
count--;
System.out.println("删除成功!");
}
}
// 修改学生
public static void updateStudent(Scanner scanner, Student[] students, int count) {
System.out.print("请输入要修改的学生的学号:");
String id = scanner.next();
int index = findStudentIndexById(id, students, count);
if (index == -1) {
System.out.println("该学生不存在!");
} else {
System.out.print("请输入新的姓名(原姓名:" + students[index].getName() + "):");
String name = scanner.next();
System.out.print("请输入新的年龄(原年龄:" + students[index].getAge() + "):");
int age = scanner.nextInt();
System.out.print("请输入新的性别(原性别:" + students[index].getGender() + "):");
String gender = scanner.next();
System.out.print("请输入新的班级(原班级:" + students[index].getClazz() + "):");
String clazz = scanner.next();
students[index].setName(name);
students[index].setAge(age);
students[index].setGender(gender);
students[index].setClazz(clazz);
System.out.println("修改成功!");
}
}
// 查询学生
public static void queryStudent(Scanner scanner, Student[] students, int count) {
System.out.print("请输入要查询的学生的学号:");
String id = scanner.next();
int index = findStudentIndexById(id, students, count);
if (index == -1) {
System.out.println("该学生不存在!");
} else {
System.out.println(students[index]);
}
}
// 显示所有学生
public static void showAllStudents(Student[] students, int count) {
for (int i = 0; i < count; i++) {
System.out.println(students[i]);
}
}
// 根据学号查找学生在数组中的索引位置
public static int findStudentIndexById(String id, Student[] students, int count) {
for (int i = 0; i < count; i++) {
if (students[i].getId().equals(id)) {
return i;
}
}
return -1;
}
}
class Student {
private String id; // 学号
private String name; // 姓名
private int age; // 年龄
private String gender; // 性别
private String clazz; // 班级
public Student(String id, String name, int age, String gender, String clazz) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.clazz = clazz;
}
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;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",年龄:" + age + ",性别:" + gender + ",班级:" + clazz;
}
}
```
这个学生管理系统实现了添加学生、删除学生、修改学生、查询学生、显示所有学生等功能。你可以根据自己的需要进行修改和扩展。
阅读全文