基于java的学生管理系统
时间: 2023-12-23 22:27:47 浏览: 63
基于Java的学生管理系统通常包含基本的增删改查功能。下面是一个简单的示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
class Student {
private String name;
private int age;
private String id;
public Student(String name, int age, String id) {
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public int getAge {
return age;
}
public String getId() {
return id;
}
}
class StudentManagementSystem {
private ArrayList<Student> students;
public StudentManagementSystem() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void deleteStudent(String id) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.remove(i);
break;
}
}
}
public void updateStudent(String id, Student newStudent) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.set(i, newStudent);
break;
}
}
}
public void searchStudent(String id) {
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println("Name: " + student.getName());
System.out.println("Age: " + student.getAge());
System.out.println("ID: " + student.getId());
break;
}
}
}
}
public class Main {
public static void main(String[] args) {
StudentManagementSystem sms = new StudentManagementSystem();
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();
scanner.nextLine(); // 清空输入缓冲区
switch (choice) {
case 1:
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine(); // 清空输入缓冲区
System.out.println("请输入学生学号:");
String id = scanner.nextLine();
Student newStudent = new Student(name, age, id);
sms.addStudent(newStudent);
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入要删除的学生学号:");
String deleteId = scanner.nextLine();
sms.deleteStudent(deleteId);
System.out.println("删除成功!");
break;
case 3:
System.out.println("请输入要修改的学生学号:");
String updateId = scanner.nextLine();
System.out.println("请输入新的学生姓名:");
String newName = scanner.nextLine();
System.out.println("请输入新的学生年龄:");
int newAge = scanner.nextInt();
scanner.nextLine(); // 清空输入缓冲区
System.out.println("请输入新的学生学号:");
String newId = scanner.nextLine();
Student updatedStudent = new Student(newName, newAge, newId);
sms.updateStudent(updateId, updatedStudent);
System.out.println("修改成功!");
break;
case 4:
System.out.println("请输入要查找的学生学号:");
String searchId = scanner.nextLine();
sms.searchStudent(searchId);
break;
case 0:
System.exit(0);
break;
default:
System.out.println("无效的选择!");
break;
}
}
}
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)