如何让数组支撑Java选课系统的增删查退所有方法?
Got it, let's work through how to use a single array to handle all your course management operations while following those rules. The key is to structure the array to hold student-specific course data, so we'll start with a simple data structure to link each student to their enrolled courses.
Step 1: Define a Student Data Class
First, we need a way to store a student's name along with their list of courses. We'll create an inner Student class (you could also make it a separate class if you prefer) that holds these two pieces of info:
static class Student { String name; ArrayList<String> courses; Student(String name) { this.name = name; this.courses = new ArrayList<>(); } }
Step 2: Set Up the Array to Track All Students
We'll use a Student[] array to store all student records, plus a counter to keep track of how many students we have (since arrays have fixed size, we'll start with a reasonable initial capacity—you can add logic to expand it later if needed):
private static Student[] students = new Student[100]; // Initial capacity for 100 students private static int studentCount = 0; // Tracks how many students are in the array
Step 3: Modify Each Method to Use the Array
Now let's update your A, D, L methods to interact with this array, enforcing all your rules:
Add Course (A) Method
This method will:
- Check if the student already exists
- If not, create a new student record, add the course, and note it's their first course
- If they exist, verify the course isn't already enrolled before adding
public static void A() { Scanner input = new Scanner(System.in); System.out.println("Please enter the student name:"); String name = input.nextLine().trim(); System.out.println("Please enter the course you want to add:"); String course = input.nextLine().trim(); // Check if student already exists Student existingStudent = findStudentByName(name); if (existingStudent == null) { // Create new student record Student newStudent = new Student(name); newStudent.courses.add(course); students[studentCount] = newStudent; studentCount++; System.out.println("Adding course : " + course); System.out.println("This is your first course! Add course successfully"); } else { // Check for duplicate course if (existingStudent.courses.contains(course)) { System.out.println("Error: You've already enrolled in " + course + ". Cannot add duplicate course."); } else { existingStudent.courses.add(course); System.out.println("Adding course : " + course); System.out.println("Add course successfully"); } } }
Drop Course (D) Method
This method will:
- Verify the student exists
- Check if the course is in their enrolled list
- Drop the course, and keep the student record even if all courses are dropped
public static void D() { Scanner input = new Scanner(System.in); System.out.println("Please enter a student name"); String name = input.nextLine().trim(); System.out.println("please enter the course you want to drop:"); String course = input.nextLine().trim(); Student student = findStudentByName(name); if (student == null) { System.out.println("Error: No student record found for " + name + "."); return; } if (!student.courses.contains(course)) { System.out.println("Error: You are not enrolled in " + course + ". Cannot drop unenrolled course."); } else { student.courses.remove(course); System.out.println("Dropping course: " + course); System.out.println("Drop course successfully"); // Even if courses are empty, we keep the student record as required } }
List Courses (L) Method
This method will fetch and display the student's enrolled courses, or note if they have none:
public static void L() { Scanner input = new Scanner(System.in); System.out.println("Please enter the student name"); String name = input.nextLine().trim(); Student student = findStudentByName(name); if (student == null) { System.out.println("No student record found for " + name + "."); return; } System.out.println("Courses enrolled by " + name + ":"); if (student.courses.isEmpty()) { System.out.println("No courses enrolled (empty record retained)."); } else { for (String course : student.courses) { System.out.println("- " + course); } } }
Helper Method: Find Student by Name
We'll add a helper method to search the array for a student by name—this avoids repeating code in A, D, L methods:
private static Student findStudentByName(String name) { for (int i = 0; i < studentCount; i++) { if (students[i].name.equalsIgnoreCase(name)) { // Case-insensitive search, adjust if needed return students[i]; } } return null; }
Full Modified Code
Here's the complete code with all these changes put together:
import java.util.*; public class codeTraining { // Inner class to represent a student with their courses static class Student { String name; ArrayList<String> courses; Student(String name) { this.name = name; this.courses = new ArrayList<>(); } } // Single array to hold all student records private static Student[] students = new Student[100]; private static int studentCount = 0; public static void main(String[] args) { Scanner input = new Scanner(System.in); String AddDrop = ""; while (!AddDrop.equalsIgnoreCase("Q")) { // Case-insensitive quit System.out.println("\nAdd-Drop Course Menu"); System.out.println("A for Add, D for Drop, L for List, Q for Quit"); AddDrop = input.nextLine().trim(); switch (AddDrop.toUpperCase()) { // Handle lowercase inputs case "A" -> A(); case "D" -> D(); case "L" -> L(); case "Q" -> Q(); default -> System.out.println("Please enter a valid character!"); } } } public static void A() { Scanner input = new Scanner(System.in); System.out.println("Please enter the student name:"); String name = input.nextLine().trim(); System.out.println("Please enter the course you want to add:"); String course = input.nextLine().trim(); Student existingStudent = findStudentByName(name); if (existingStudent == null) { Student newStudent = new Student(name); newStudent.courses.add(course); students[studentCount] = newStudent; studentCount++; System.out.println("Adding course : " + course); System.out.println("This is your first course! Add course successfully"); } else { if (existingStudent.courses.contains(course)) { System.out.println("Error: You've already enrolled in " + course + ". Cannot add duplicate course."); } else { existingStudent.courses.add(course); System.out.println("Adding course : " + course); System.out.println("Add course successfully"); } } } public static void D() { Scanner input = new Scanner(System.in); System.out.println("Please enter a student name"); String name = input.nextLine().trim(); System.out.println("please enter the course you want to drop:"); String course = input.nextLine().trim(); Student student = findStudentByName(name); if (student == null) { System.out.println("Error: No student record found for " + name + "."); return; } if (!student.courses.contains(course)) { System.out.println("Error: You are not enrolled in " + course + ". Cannot drop unenrolled course."); } else { student.courses.remove(course); System.out.println("Dropping course: " + course); System.out.println("Drop course successfully"); } } public static void L() { Scanner input = new Scanner(System.in); System.out.println("Please enter the student name"); String name = input.nextLine().trim(); Student student = findStudentByName(name); if (student == null) { System.out.println("No student record found for " + name + "."); return; } System.out.println("Courses enrolled by " + name + ":"); if (student.courses.isEmpty()) { System.out.println("No courses enrolled (empty record retained)."); } else { for (String course : student.courses) { System.out.println("- " + course); } } } public static void Q() { System.out.println("Quit..."); } // Helper method to search for a student by name private static Student findStudentByName(String name) { for (int i = 0; i < studentCount; i++) { if (students[i].name.equalsIgnoreCase(name)) { return students[i]; } } return null; } }
Key Notes
- The single
Student[]array acts as our central storage for all student and course data—every operation reads from and writes to this array. - We added case-insensitive handling for inputs (like "q" instead of "Q") to make the system more user-friendly.
- The
findStudentByNamehelper method reduces code duplication across the add/drop/list functions. - All your rules are enforced: no duplicate adds, no dropping unenrolled courses, empty student records are retained after dropping all courses.
内容的提问来源于stack exchange,提问作者Musen

