英文:
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<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[]);
}
}
答案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("The students currently in this course are "+ 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("Computer Science");
... 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论