j ava学生管理系统代码
时间: 2023-10-16 11:11:10 浏览: 63
以下是一个基于 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(Student student) {
students.remove(student);
}
public void displayAllStudents() {
for (Student student : students) {
System.out.println(student);
}
}
public void searchStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter student name: ");
String name = scanner.nextLine();
for (Student student : students) {
if (student.getName().equalsIgnoreCase(name)) {
System.out.println(student);
return;
}
}
System.out.println("Student not found!");
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
// Add some students
system.addStudent(new Student("Alice", "CS", 3.5));
system.addStudent(new Student("Bob", "Math", 3.0));
system.addStudent(new Student("Charlie", "Physics", 3.9));
// Display all students
System.out.println("All students:");
system.displayAllStudents();
// Search for a student
System.out.println("Search for a student:");
system.searchStudent();
}
}
class Student {
private String name;
private String major;
private double gpa;
public Student(String name, String major, double gpa) {
this.name = name;
this.major = major;
this.gpa = gpa;
}
public String getName() {
return name;
}
public String getMajor() {
return major;
}
public double getGpa() {
return gpa;
}
public String toString() {
return "Name: " + name + ", Major: " + major + ", GPA: " + gpa;
}
}
```
在这个示例中,我们创建了一个 `StudentManagementSystem` 类,它可以添加、删除、显示和搜索学生。我们还创建了一个 `Student` 类,它包含学生的名称、专业和 GPA。在 `main` 方法中,我们添加了一些学生并显示了所有学生,然后搜索一个学生。
阅读全文