Prompt Title: Java 学生管理系统

Created 1 year ago
Votes 0
Views 266 times
0

Prompt Detail:

好的,我来给您提供一个简单的Java学生管理系统代码示例。

首先,我们需要创建一个Student类,用来存储学生的信息,包括姓名、年龄、性别和成绩等属性:

java
public class Student { private String name; private int age; private String gender; private double score; public Student(String name, int age, String gender, double score) { this.name = name; this.age = age; this.gender = gender; this.score = score; } 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 double getScore() { return score; } public void setScore(double score) { this.score = score; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", score=" + score + '}'; } }

然后,我们需要创建一个管理学生的类,可以实现添加学生、删除学生、修改学生信息和查找学生等功能。这里我们使用List来存储学生的信息:

java
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class StudentManager { private List<Student> students = new ArrayList<>(); public void addStudent(Student student) { students.add(student); System.out.println("添加成功!"); } public void removeStudent(String name) { 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("没有找到该学生!"); } public void updateStudent(Student student) { for (int i = 0; i < students.size(); i++) { if (students.get(i).getName().equals(student.getName())) { students.set(i, student); System.out.println("修改成功!"); return; } } System.out.println("没有找到该学生!"); } public void searchStudent(String name) { for (int i = 0; i < students.size(); i++) { if (students.get(i).getName().equals(name)) { System.out.println(students.get(i)); return; } } System.out.println("没有找到该学生!"); } public void displayStudents() { for (int i = 0; i < students.size(); i++) { System.out.println(students.get(i)); } } public static void main(String[] args) { StudentManager manager = new StudentManager(); 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("5. 显示所有学生
avatar
Nick
Shared 5 prompts
Created 1 year ago

Leave a Comment

Related Tag Prompts

0
0
디자인닉네임
1 year ago 2023-03-09 09:42:33 lailah
0
0
0
0
Tên gọi
1 year ago 2023-09-16 14:58:15 Alicia Cao