java做一个学生管理系统
时间: 2024-05-28 12:05:18 浏览: 95
Java可以用来开发学生管理系统,以下是一个简单的示例:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
private List<Student> students;
public StudentManagementSystem() {
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
public void displayAllStudents() {
for (Student student : students) {
System.out.println(student);
}
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Add student");
System.out.println("2. Remove student");
System.out.println("3. Display all students"); System.out.println("4. 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: ");
int id = scanner.nextInt();
Student student = new Student(name, id);
system.addStudent(student);
System.out.println("Student added successfully!");
break;
case 2:
System.out.print("Enter student ID to remove: ");
int removeId = scanner.nextInt();
Student removeStudent = null;
for (Student s : system.students) {
if (s.getId() == removeId) {
removeStudent = s;
break;
}
}
if (removeStudent != null) {
system.removeStudent(removeStudent);
System.out.println("Student removed successfully!");
} else {
System.out.println("Student not found!");
}
break;
case 3:
system.displayAllStudents();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
}
}
}
class Student {
private String name;
private int id;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
```
这个学生管理系统使用了Java的面向对象编程思想,通过一个`StudentManagementSystem`类来管理学生对象。你可以通过选择菜单选项来添加学生、删除学生和显示所有学生。每个学生对象包含姓名和学号。
阅读全文
相关推荐
















