用Java写一个学生管理系统
时间: 2023-10-27 15:05:54 浏览: 124
好的,我可以为您提供一个简单的学生管理系统的Java代码示例。以下是一个基本的学生管理系统,其中包含添加学生、删除学生和显示学生信息等功能:
```
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManagementSystem {
private ArrayList<Student> students = new ArrayList<Student>();
public void addStudent(Student student) {
students.add(student);
}
public void deleteStudent(Student student) {
students.remove(student);
}
public void displayAllStudents() {
for (Student student : students) {
System.out.println(student.toString());
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManagementSystem system = new StudentManagementSystem();
while (true) {
System.out.println("1. Add Student");
System.out.println("2. Delete Student");
System.out.println("3. Display All Students");
System.out.println("4. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Enter Student Name:");
String name = scanner.next();
System.out.println("Enter Student Roll No:");
int rollNo = scanner.nextInt();
Student student = new Student(name, rollNo);
system.addStudent(student);
break;
case 2:
System.out.println("Enter Student Roll No:");
int deleteRollNo = scanner.nextInt();
for (Student s : system.students) {
if (s.getRollNo() == deleteRollNo) {
system.deleteStudent(s);
break;
}
}
break;
case 3:
system.displayAllStudents();
break;
case 4:
System.out.println("Exiting System");
System.exit(0);
default:
System.out.println("Invalid Choice");
}
}
}
}
class Student {
private String name;
private int rollNo;
public Student(String name, int rollNo) {
this.name = name;
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public int getRollNo() {
return rollNo;
}
public String toString() {
return "Name: " + name + ", Roll No: " + rollNo;
}
}
```
在这个示例中,我们创建了一个 `StudentManagementSystem` 类来管理学生信息。该类包含了添加、删除和显示学生信息的方法。我们还创建了一个 `Student` 类来表示学生的姓名和学号。在 `main` 方法中,我们使用了一个简单的命令行菜单来提供用户与系统交互的方式。
希望这个示例能够帮助您开始编写自己的学生管理系统!
阅读全文