【发布时间】:2021-02-06 12:36:14
【问题描述】:
问题是我需要打印对象的信息,包括包含学生分数的数组。但它反而会扔垃圾。数组也必须有 5 个元素
public class Student {
Student(String fullname, int group, int marks[]){
this.fullname = fullname;
this.group = group;
this.marks = marks;
}//constructor
void display(){
System.out.println("Named: " + fullname + " | Group: " + group + " | Marks: " + marks);//prints out the objects
}
int group;
int marks[];
String fullname;//class fields
public static void main(String[] args){
Student Matthew = new Student("Whatever 1",14, new int[]{9, 6, 8, 4, 5});
Student John = new Student("Whatever 2",13, new int[]{4, 9, 5, 10, 7});
Student Max = new Student("Whatever 3",14, new int[]{7, 10, 8, 9, 9});//objects
Matthew.display();
John.display();
Max.display();
}
}
【问题讨论】:
-
使用 Arrays::toString。 docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/…
-
Arrays.toString(marks) -
不,因为我试图使用构造函数打印数组
-
而不是告诉我们“但是它会抛出垃圾。”,向我们展示它实际打印的内容......
标签: java arrays class oop constructor