英文:
Unable to print the array which is passed to the constructor
问题
问题在于我需要打印包含学生分数的数组在内的对象信息,但它却输出了垃圾内容。而且这些数组必须有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();
}
}
英文:
The problem is that I need to print objects' info including arrays which contain students' marks. But it throws rubbish instead. Also arrays must have 5 elements
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();
}
}
答案1
得分: 0
使用Arrays.toString(marks)来打印数组数值
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: " + Arrays.toString(marks));
}
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();
}
}
英文:
Use Arrays.toString(marks) to print the array values
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: " + Arrays.toString(marks));
}
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();
}
}
答案2
得分: 0
如果出于美观原因不想使用Arrays.toString()
方法,您可以使用以下方法:
public static String arrayToString(int[] array) {
String arrayString = "";
for (int i = 0; i < array.length; i++) {
arrayString += array[i] + ", "; // 在这里写入您希望用来分隔数组元素的内容
}
return arrayString;
}
英文:
If your'e not want to use the Arrays.toString()
method (For aesthetic reasons) you can use that method:
public static String arrayToString(int[] array) {
String arrayString = "";
for (int i = 0; i < array.length; i++) {
arrayString += array[i] + ", "; // Write here whatever you want to separate the array elements
}
return arrayString;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论