调用时字符串不会显示

时间:2017-10-12 06:45:12

标签: java android

我有一个问题,即使输入了信息,当被要求显示时(选项6)它也没有显示从选项6输入的信息。我觉得它是因为我不能添加超级(courseName) )在我的courseModule类中。非常感谢任何帮助,生病留下以下所有相关代码。

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 public courseModule有关,但是我尝试过的所有内容都没有用,任何帮助都非常感谢被困30分钟而在别处找不到帮助

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:0)

public class CourseModule implements Course {

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


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

    @Override
    public void assignTeacher(Teacher teacher) {

    }

    @Override
    public void enrolStudent(Student student) {

    }

    public String toString(){
        // i don't know what method's are in your teacher class, so im just guessing here.
        return courseName + " " + teacher.name;
    }
}

为了让你从toString方法打印实际值,你必须添加自己的toString方法。