无法打印传递给构造函数的数组。

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

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 = &quot;&quot;;
		for (int i = 0; i &lt; array.length; i++) {
			arrayString += array[i] + &quot;, &quot;; // Write here whatever you want to separate the array elements
		}
		return arrayString;
	}

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

发表评论

匿名网友

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

确定