可以用java帮我写一个学生管理系统吗?能实现增删改查
时间: 2024-05-01 07:17:01 浏览: 99
当然可以!以下是一个简单的学生管理系统示例代码,实现了增删改查的功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
static ArrayList<Student> students = new ArrayList<Student>();
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("欢迎使用学生管理系统!");
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. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
removeStudent();
break;
case 3:
updateStudent();
break;
case 4:
queryStudent();
break;
case 5:
System.out.println("谢谢使用!");
System.exit(0);
}
}
}
public static void addStudent() {
System.out.println("请输入学生信息:");
System.out.print("姓名:");
String name = scanner.next();
System.out.print("学号:");
String id = scanner.next();
System.out.print("性别:");
String gender = scanner.next();
System.out.print("年龄:");
int age = scanner.nextInt();
System.out.print("成绩:");
double score = scanner.nextDouble();
Student student = new Student(name, id, gender, age, score);
students.add(student);
System.out.println("添加成功!");
}
public static void removeStudent() {
System.out.print("请输入要删除的学生学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (id.equals(student.getId())) {
students.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("该学生不存在!");
}
public static void updateStudent() {
System.out.print("请输入要修改的学生学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (id.equals(student.getId())) {
System.out.println("请输入新的学生信息:");
System.out.print("姓名:");
String name = scanner.next();
System.out.print("性别:");
String gender = scanner.next();
System.out.print("年龄:");
int age = scanner.nextInt();
System.out.print("成绩:");
double score = scanner.nextDouble();
student.setName(name);
student.setGender(gender);
student.setAge(age);
student.setScore(score);
System.out.println("修改成功!");
return;
}
}
System.out.println("该学生不存在!");
}
public static void queryStudent() {
System.out.print("请输入要查询的学生学号:");
String id = scanner.next();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (id.equals(student.getId())) {
System.out.println(student);
return;
}
}
System.out.println("该学生不存在!");
}
}
class Student {
private String name;
private String id;
private String gender;
private int age;
private double score;
public Student(String name, String id, String gender, int age, double score) {
this.name = name;
this.id = id;
this.gender = gender;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "姓名:" + name + "\n学号:" + id + "\n性别:" + gender + "\n年龄:" + age + "\n成绩:" + score;
}
}
```
在这个示例代码中,我们使用了一个静态的 ArrayList 对象来存储学生信息。在主函数中,我们使用了一个 while 循环来不断接受用户输入的操作,根据用户的选择来执行相应的函数。在每个函数中,我们使用 Scanner 对象来接受用户的输入,并根据输入来进行相应的操作,例如添加、删除、修改或查询学生信息。最后,我们使用 Student 类来封装学生信息,方便操作。
阅读全文