我有一个问题,即使输入了信息,当要求显示它(选项6)时,它也不会显示从选项6输入的信息。我感觉像是它的原因是我无法在courseModule类中添加super(courseName) 。非常感谢您的帮助,请在下面留下所有相关代码。

AppMain类

    import java.util.*;
public class AppMain {
// ArrayList of students
private static ArrayList<Student> studentList = new ArrayList<Student>();
// ArrayList of teachers
private static ArrayList<Teacher> teacherList = new ArrayList<Teacher>();
// ArrayList of Modules
private static ArrayList<CourseModule> ModuleList = new ArrayList<CourseModule>();


public static void main(String[] args)
{
    menu();
}

public static void menu()
{




    int option = 0;

    while(option != 7) {

        Scanner in = new Scanner(System.in);

        // Creates main menu
        System.out.println("Choose an option.");
        System.out.println("[1] Create student");
        System.out.println("[2] Display students");
        System.out.println("[3] Create Teacher");
        System.out.println("[4] Display Teachers");
        System.out.println("[5] Create Module");
        System.out.println("[6] Display Modules");
        System.out.println("[7] Exit");
        System.out.print("Option? ");
        option = in.nextInt();

        // Switch case for dealing with choice
        // Each case is braced {} so convinient variable names can be reused..
        // .. without fear of variables 'carrying over'
        switch (option) {
            case 1: {
                String name;
                int grade;
                System.out.println("Enter name: ");
                in.nextLine(); // Stops the program 'running' over the next line
                name = in.nextLine();
                System.out.println("Enter grade: ");
                grade = in.nextInt();
                if (grade <= 100 && grade >= 0) {
                    Student student = new Student(name, grade);
                    studentList.add(student);
                    //error message
                } else {
                    System.out.println("Grade not in boundaries!\nUser not added \nEnter grade between 0-100.");
                }
                break;
            }
            case 2: {
                Iterator<Student> itr = studentList.iterator();
                while (itr.hasNext()) {
                    Student student = itr.next();
                    System.out.println(student.toString() + "\n");
                }
                break;
            }
            case 3: {
                String name;
                System.out.println("Enter Teachers name: ");
                in.nextLine();
                name = in.nextLine();
                Teacher teacher = new Teacher(name);
                teacherList.add(teacher);
                break;
            }
            case 4: {
                // Iterators through the teachList ArrayList..
                // .. and prints each one with a line between
                Iterator<Teacher> itr = teacherList.iterator();
                while (itr.hasNext()) {
                    Teacher teacher = itr.next();
                    System.out.println(teacher.toString() + "\n");
                }
                break;
            }
            case 5: {

                String name;
                String courseName; // defining module String
                System.out.println("Enter Teachers name: ");
                in.nextLine();
                name = in.nextLine();
                Teacher teacher = new Teacher(name);
                teacherList.add(teacher);
                System.out.println("Enter Module Name: ");
                courseName = in.nextLine();
                CourseModule Module = new CourseModule(courseName, teacher);
                ModuleList.add(Module);
                break;



            }
            case 6: {
              // Iterator<Teacher> itr = teacherList.iterator();
             //  while (itr.hasNext()) {
              //      Teacher teacher = itr.next();
             //      System.out.println(teacher.toString() + "\n");
             // }
                Iterator<CourseModule> itr = ModuleList.iterator();
                while (itr.hasNext()) {
                    CourseModule Module = itr.next();
                   System.out.println(Module.toString() + "\n");
                }


这是CourseModule类

    import java.util.*;
public class CourseModule implements Course {

String courseName;
Teacher teacher;
ArrayList<Student> students = new ArrayList<Student>();


public CourseModule (String courseName, Teacher teacher) {

}

@Override
public void assignTeacher(Teacher teacher) {


}

@Override
public void enrolStudent(Student student) {

}


我觉得它与CourseModule公共courseModule有关,但是我尝试过的所有方法都不起作用,任何帮助都被困了30分钟而在其他地方却无济于事

最佳答案

您的构造函数中没有代码,因此更改为

public CourseModule (String courseName, Teacher teacher) {
    this.courseName = courseName;
    this.teacher = teacher;
}


在某个阶段,您也需要填写其他方法

10-01 23:47