在测试类中打印一个数组。

huangapple go评论64阅读模式
英文:

Print an array in tester class

问题

我试图在Course类的测试类中返回学生数组,但是我一直收到一个“class expected”错误。我尝试过使用 students[].TestCourse,但那也不起作用。

public class Course {
    private String courseName;
    private String[] students = new String[4];
    private int numberOfStudents;

    public Course(String courseName) {
        this.courseName = courseName;
    }

    public void addStudent(String student) {
        if (numberOfStudents == students.length) {
            String[] copy = new String[students.length * 2];
            System.arraycopy(students, 0, copy, 0, students.length);
            students = copy;
        }

        students[numberOfStudents] = student;
        numberOfStudents++;
    }

    public String[] getStudents() {
        return students;
    }

    public void dropStudent(String student) {
        for (int i = 0; i < students.length; i++) {
            if (students[i] == student) {
                students[i] = null;
            }
            for (i = i; i < students.length - 1; i++) {
                students[i] = students[i + 1];
            }
        }
    }
}

public class TestCourse {
    public static void main() {
        Course compScience = new Course("Computer Science");
        compScience.addStudent("Jack");
        compScience.addStudent("Dean");
        compScience.addStudent("Leon");
        compScience.dropStudent("Dean");
        System.out.println("The students currently in this course are " + students[]);
    }
}
英文:

I am trying to return the array of students in a tester class for the Course class but I keep getting a .class expected error. I've tried to do
students[].TestCourse but that doesn't work either.

public class Course {
private String courseName;
private String[] students = new String[4];
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
if (numberOfStudents == students.length) {
String [] copy = new String [students.length*2];
System.arraycopy(students,0,copy,0,students.length);
students = copy;
}
students[numberOfStudents] = student;
numberOfStudents++;
}
public String[] getStudents() {
return students;
}
public void dropStudent(String student) {
for (int i=0;i&lt;students.length;i++) {
if (students[i]==student) {
students[i] = null;
}
for (i=i;i&lt;students.length-1;i++) {
students[i] = students[i+1];
}
}
}
}
public class TestCourse {
public static void main() {
Course compScience = new Course(&quot;Computer Science&quot;);
compScience.addStudent(&quot;Jack&quot;);
compScience.addStudent(&quot;Dean&quot;);
compScience.addStudent(&quot;Leon&quot;);
compScience.dropStudent(&quot;Dean&quot;);
System.out.println(&quot;The students currently in this course are &quot;+ students[]);
}
}

答案1

得分: 0

将您的main方法中的这行代码更改为:

System.out.println("目前在这门课程中的学生有:" + Arrays.toString(compScience.getStudents()));

然后它应该能够正常运行!

您所做的只是尝试直接调用类的字段,而您需要通过您创建的引用来访问它,即Course compScience = new Course("计算机科学")...然后调用它的方法getStudents(),如下所示compScience.getStudents()。要获取数组的内容,您需要将此方法调用包装在Arrays.toString()中,如上所示。

英文:

Change the line in your main method to:

System.out.println(&quot;The students currently in this course are &quot;+ Arrays.toString(compScience.getStudents()));

and it should fire up!

All you've done is try to call the field of a class directly when you need to access it via the reference you created Course compScience = new Course(&quot;Computer Science&quot;); ... then call it's method getStudents() as follows compScience.getStudents(). To get the contents of the array you then need to wrap this method call in Arrays.toString() as above.

huangapple
  • 本文由 发表于 2020年10月23日 20:07:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64499691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定