j ava制作学生管理系统
时间: 2024-05-11 22:19:34 浏览: 102
java做的学生管理系统
Java可以用来制作学生管理系统,以下是一个简单的示例:
```java
import java.util.Scanner;
public class StudentManagementSystem {
private static final int MAX_STUDENTS = 100;
private Student[] students;
private int numStudents;
public StudentManagementSystem() {
students = new Student[MAX_STUDENTS];
numStudents = 0;
}
public void addStudent(Student s) {
if (numStudents < MAX_STUDENTS) {
students[numStudents] = s;
numStudents++;
System.out.println("Student added successfully.");
} else {
System.out.println("Maximum number of students reached.");
}
}
public void removeStudent(int id) {
int index = findStudentIndex(id);
if (index != -1) {
for (int i = index; i < numStudents - 1; i++) {
students[i] = students[i + 1];
}
numStudents--;
System.out.println("Student removed successfully.");
} else {
System.out.println("Student not found.");
}
}
public void printStudentList() {
if (numStudents == 0) {
System.out.println("No students in the system.");
} else {
System.out.println("Student list:");
for (int i = 0; i < numStudents; i++) {
System.out.println(students[i]);
}
}
}
private int findStudentIndex(int id) {
for (int i = 0; i < numStudents; i++) {
if (students[i].getId() == id) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
StudentManagementSystem sms = new StudentManagementSystem();
Scanner scanner = new Scanner(System.in);
int choice = 0;
while (choice != 4) {
System.out.println("1. Add student");
System.out.println("2. Remove student");
System.out.println("3. Print student list");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter student ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // consume newline
System.out.print("Enter student name: ");
String name = scanner.nextLine();
sms.addStudent(new Student(id, name));
break;
case 2:
System.out.print("Enter student ID: ");
id = scanner.nextInt();
sms.removeStudent(id);
break;
case 3:
sms.printStudentList();
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice.");
}
System.out.println();
}
scanner.close();
}
}
class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String toString() {
return "ID: " + id + ", Name: " + name;
}
}
```
这是一个简单的学生管理系统,可以添加、删除和打印学生列表。你可以根据自己的需求进行修改和扩展。
阅读全文