gpt4 book ai didi

java - 调用时不会显示字符串

转载 作者:行者123 更新时间:2023-12-01 20:11:06 26 4
gpt4 key购买 nike

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

App主类

    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 分钟,在其他地方找不到帮助

最佳答案

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

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

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

关于java - 调用时不会显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46703080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com