gpt4 book ai didi

java - 多类 Java 错误 : Ljava. lang.String

转载 作者:行者123 更新时间:2023-12-04 09:55:24 25 4
gpt4 key购买 nike

我必须创建 2 个类(Course.java 和 UseCourse.java)。我必须有像 get course、courseName、addStudent、dropStudent、getStudent 和 getNumberOfStudent 这样的方法。

当我尝试运行这些文件时,我不断收到:

3名学生在Java编程:[Ljava.lang.String; @6bc7c054

有人可以解释这意味着什么以及我如何解决它吗?

使用类(class).java

public class UseCourse{
public static void main(String[] args){
Course c1 = new Course("Java Programming", "Bob Brown");

c1.getCourseName();
c1.getStudents();
c1.getNumberOfStudents();

System.out.println(c1.numberOfStudents + " students are in "+
c1.courseName + " : " + c1.students);

//c1.dropStudent("Andrew Drew");
c1.getStudents();
c1.getNumberOfStudents();
System.out.println(c1.numberOfStudents + " students are in "+
c1.courseName + " : " + c1.students);
}
}


类(class).java
import java.util.Arrays;
import java.lang.String;

public class Course{
String courseName;
String[] students = {"Berry Cherry", "Lia Lee", "Andrew Drew"};
int numberOfStudents;

Course(String newCourseName, String newAddStudent){
courseName = newCourseName;

// Not sure if I'm doing this right but I'm trying to
// add the new student to the end of the list/array

students[students.length -1] = newAddStudent;
}

/*public void dropStudent{

}*/

public String getCourseName(){
return courseName;
}

public String getStudents(){
return Arrays.toString(students);
}

public int getNumberOfStudents(){
numberOfStudents = students.length;
return numberOfStudents;
}
}


最佳答案

因为这是数组的默认文本表示( toString )

所以使用这个:Arrays.toString(students)

System.out.println(c1.numberOfStudents + " students are in "+
c1.courseName + " : " + Arrays.toString(c1.students));

关于java - 多类 Java 错误 : Ljava. lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61931046/

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