用JAVA编写一个学生信息管理系统
时间: 2023-09-15 11:20:56 浏览: 84
好的,下面是一个简单的学生信息管理系统的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(int index) {
students.remove(index);
}
public void displayAllStudents() {
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i).toString());
}
}
public void displayStudent(int index) {
if (index >= 0 && index < students.size()) {
System.out.println(students.get(index).toString());
} else {
System.out.println("Invalid index.");
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManagementSystem sms = new StudentManagementSystem();
while (true) {
System.out.println("\n********** Student Management System **********");
System.out.println("1. Add Student");
System.out.println("2. Remove Student");
System.out.println("3. Display All Students");
System.out.println("4. Display Student");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter student name: ");
String name = scanner.next();
System.out.print("Enter student ID: ");
String id = scanner.next();
System.out.print("Enter student major: ");
String major = scanner.next();
Student student = new Student(name, id, major);
sms.addStudent(student);
System.out.println("Student added successfully.");
break;
case 2:
System.out.print("Enter index of student to remove: ");
int index = scanner.nextInt();
sms.removeStudent(index);
System.out.println("Student removed successfully.");
break;
case 3:
sms.displayAllStudents();
break;
case 4:
System.out.print("Enter index of student to display: ");
index = scanner.nextInt();
sms.displayStudent(index);
break;
case 5:
System.out.println("Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice.");
}
}
}
}
class Student {
private String name;
private String id;
private String major;
public Student(String name, String id, String major) {
this.name = name;
this.id = id;
this.major = major;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getMajor() {
return major;
}
public String toString() {
return "Name: " + name + ", ID: " + id + ", Major: " + major;
}
}
```
该程序实现了以下功能:
1. 添加学生信息
2. 删除学生信息
3. 显示所有学生信息
4. 显示特定学生信息
5. 退出程序
在程序运行时,用户可以选择要执行的操作。添加学生信息时,用户需要输入学生的姓名、学号和专业。删除学生信息时,用户需要输入要删除的学生在列表中的索引。显示特定学生信息时,用户需要输入要显示的学生在列表中的索引。
阅读全文