首页 > 解决方案 > 如何正确调用另一个类的构造函数,然后将其作为数组打印到控制台?

问题描述

我试图通过调用另一个类的构造函数并在我的主类中初始化一个数组来将 5 组学生姓名和分数打印到控制台

当前代码显示了我尝试调用构造函数并初始化数组的方式

学生班级:

     package studentinfo;

public class Student {

String name;    
int score;    

public Student (String sname, int sscore) {
sname= name; 
sscore = score; 

}
    public void greeting() {
        //Student 1 
        Student student1 = new Student ("Mike" , 40);
        System.out.println("My name is: " + student1.name + " " +"My score is:" + student1.score);
        //Student 2
        Student student2 = new Student ("Tom" , 50);
        System.out.println("My name is: " + student2.name + " " +"My score is:" + student2.score);
        //Student 3
        Student student3 = new Student ("John" , 60);
        System.out.println ("My name is: " + student3.name +" " +",My score is:" +student3.score); 
        //Student 4
        Student student4 = new Student ("May" , 80);
        System.out.println ("My name is: " + student4.name +" " +",My score is:" +student4.score); 
        //Student 5 
        Student student5 = new Student ("Lucy" , 50);
        System.out.println ("My name is: " + student5.name +" " +",My score is:" +student5.score); 
    }
}

学生信息类:

    **package studentinfo;
import java.util.Arrays;

public class StudentInfo {

    static Student[] students;
    static int maxStudentNumber; 

    public static void main(String[] args) {

        addStudents();

        int maxStudentNumber = 5;

        printStudents();

        System.out.println ("The maximum amount of students per class is: " + maxStudentNumber +"" );

    }

    static void addStudents() {
    Student students = new Student(" " , 5);

    }
    static void printStudents() {
    students[1].greeting();
    students[2].greeting();
    students[3].greeting();
    students[4].greeting();
    students[5].greeting();

    System.out.println (Arrays.toString(students));         
    }
}**

标签: javanetbeans

解决方案


你的学生类应该是这样的:

public class Student {

    String name;
    int score;

    public Student (String sname, int sscore) {
        name = sname;
        score = sscore;

    }
    public void greeting() {
        System.out.println("My name is: " + this.name + " " +"My score is:" + this.score);
    }
}

当您调用greeting()时,只有调用它的学生才能打招呼。不是所有的Student

您可以将StudentInfo类更改为:

public class StudentInfo {
    static Student[] students;
    static int maxStudentNumber;

    public static void main(String[] args) {

        maxStudentNumber = 5;
        addStudents();

        printStudents();

        System.out.println ("The maximum amount of students per class is: " + maxStudentNumber +"" );
    }

    static void addStudents() {

        students = new Student[maxStudentNumber];
        students[0] = new Student ("Mike" , 40);
        students[1] = new Student ("Tom" , 50);
        students[2] = new Student ("John" , 60);
        students[3] = new Student ("May" , 80);
        students[4] = new Student ("Lucy" , 50);
    }
    static void printStudents() {
        for(int i=0;i<maxStudentNumber;i++) {
            students[i].greeting();
        }
    }
}

希望它对你有用。


推荐阅读