java学生管理系统
时间: 2023-07-09 14:19:51 浏览: 83
Java课程设计
以下是一个简单的Java学生管理系统的示例代码,实现了添加、删除、修改和查询学生信息的功能:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
private List<Student> studentList;
public StudentManagementSystem() {
studentList = new ArrayList<>();
}
public void addStudent() {
Scanner scanner = new Scanner(System.in);
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();
Student student = new Student(name, age, gender, id);
studentList.add(student);
System.out.println("添加学生成功!");
}
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的学生学号:");
String id = scanner.nextLine();
for (Student student : studentList) {
if (student.getId().equals(id)) {
studentList.remove(student);
System.out.println("删除学生成功!");
return;
}
}
System.out.println("未找到对应学生!");
}
public void updateStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的学生学号:");
String id = scanner.nextLine();
for (Student student : studentList) {
if (student.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();
student.setName(name);
student.setAge(age);
student.setGender(gender);
System.out.println("修改学生成功!");
return;
}
}
System.out.println("未找到对应学生!");
}
public void queryStudent() {
for (Student student : studentList) {
System.out.println(student);
}
}
public void showMenu() {
System.out.println("欢迎使用学生管理系统!");
System.out.println("1. 添加学生信息");
System.out.println("2. 删除学生信息");
System.out.println("3. 修改学生信息");
System.out.println("4. 查询学生信息");
System.out.println("0. 退出程序");
System.out.print("请输入操作编号:");
}
public void run() {
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag) {
showMenu();
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
updateStudent();
break;
case 4:
queryStudent();
break;
case 0:
flag = false;
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.run();
}
}
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 "姓名:" + name + ", 年龄:" + age + ", 性别:" + gender + ", 学号:" + id;
}
}
```
该程序使用了List集合存储学生对象集合,并且熟练使用了List集合的中常用方法实现相关的操作。运行该程序后,会显示一个菜单,根据需要选择相应的操作即可完成相应功能。
阅读全文