可以帮我写一份Java关于学生管理系统的代码吗
时间: 2024-01-22 19:02:02 浏览: 29
当然可以,以下是一个简单的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(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
public void printAllStudents() {
for (Student student : students) {
System.out.println(student);
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("请选择您要进行的操作:");
System.out.println("1. 添加学生");
System.out.println("2. 删除学生");
System.out.println("3. 查看所有学生");
System.out.println("4. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
Student student = new Student(name, age);
system.addStudent(student);
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入要删除的学生姓名:");
String deleteName = scanner.next();
for (Student s : system.students) {
if (s.getName().equals(deleteName)) {
system.removeStudent(s);
System.out.println("删除成功!");
break;
}
}
break;
case 3:
system.printAllStudents();
break;
case 4:
flag = false;
System.out.println("谢谢使用!");
break;
default:
System.out.println("您的输入有误,请重新输入!");
break;
}
}
scanner.close();
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String toString() {
return "姓名:" + name + ",年龄:" + age;
}
}
```
这份代码实现了一个简单的学生管理系统,可以添加学生、删除学生、查看所有学生等操作。需要注意的是,这份代码只是一个简单的示例,实际的学生管理系统需要根据具体需求进行设计和实现。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)