java实现学生管理系统(附完整源码)
时间: 2023-11-11 19:03:25 浏览: 48
很好,我可以通过编写代码来回答这个问题。这个学生管理系统包括添加学生、删除学生、修改学生信息、查询学生信息等基本功能。以下是完整的Java源代码。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> students = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.start();
}
private void start() {
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.print("请选择:");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
updateStudent();
break;
case 4:
queryStudent();
break;
case 5:
System.out.println("退出系统");
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新选择");
break;
}
}
}
private void addStudent() {
System.out.print("请输入学生姓名:");
String name = scanner.nextLine();
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入学生性别:");
String gender = scanner.nextLine();
System.out.print("请输入学生学号:");
String id = scanner.nextLine();
students.add(new Student(name, age, gender, id));
System.out.println("添加学生成功");
}
private void deleteStudent() {
System.out.print("请输入学生学号:");
String id = scanner.nextLine();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
students.remove(i);
System.out.println("删除学生成功");
return;
}
}
System.out.println("学生不存在");
}
private void updateStudent() {
System.out.print("请输入学生学号:");
String id = scanner.nextLine();
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId().equals(id)) {
System.out.print("请输入学生姓名:");
String name = scanner.nextLine();
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入学生性别:");
String gender = scanner.nextLine();
students.set(i, new Student(name, age, gender, id));
System.out.println("修改学生信息成功");
return;
}
}
System.out.println("学生不存在");
}
private void queryStudent() {
System.out.print("请输入学生学号:");
String id = scanner.nextLine();
for (Student student : students) {
if (student.getId().equals(id)) {
System.out.println(student);
return;
}
}
System.out.println("学生不存在");
}
private static class Student {
private String name;
private int age;
private String gender;
private String id;
public Student(String name, int age, String gender, String id) {
this.name = name;
this.age = age;
this.gender = gender;
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 getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "学号:" + id + ",姓名:" + name + ",年龄:" + age + ",性别:" + gender;
}
}
}
```
这是一个简单的控制台应用程序。它使用一个ArrayList来存储学生对象,并提供了四个基本功能。你可以根据需要进行扩展和修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)