Java 学生信息管理系统
时间: 2023-12-27 07:04:47 浏览: 88
以下是一个简单的Java学生信息管理系统的示例:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> studentList;
public StudentManagementSystem() {
studentList = new ArrayList<>();
}
public void addStudent(Student student) {
studentList.add(student);
}
public void deleteStudent(String id) {
for (int i = 0; i < studentList.size(); i++) {
Student student = studentList.get(i);
if (student.getId().equals(id)) {
studentList.remove(i);
System.out.println("学生信息删除成功!");
return;
}
}
System.out.println("该学生信息不存在,请重新输入:");
}
public void viewAllStudents() {
for (Student student : studentList) {
student.printInformation();
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
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. 退出");
int choice = scanner.nextInt();
scanner.nextLine(); // 消耗换行符
switch (choice) {
case 1:
System.out.println("请输入学生的学号:");
String id = scanner.nextLine();
System.out.println("请输入学生的姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生的年龄:");
int age = scanner.nextInt();
scanner.nextLine(); // 消耗换行符
Student student = new Student(id, name, age);
system.addStudent(student);
System.out.println("学生信息添加成功!");
break;
case 2:
System.out.println("请输入要删除的学生的学号:");
String deleteId = scanner.nextLine();
system.deleteStudent(deleteId);
break;
case 3:
system.viewAllStudents();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("无效的选择,请重新输入:");
break;
}
}
}
}
class Student {
private String id;
private String name;
private int age;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void printInformation() {
System.out.println("学号:" + id);
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println();
}
}
```
阅读全文