简体   繁体   中英

How to call constructor from another class correctly and then print it as an array to console?

I am trying to print 5 sets of student names and scores to the console by calling a constructor from another class and initialising an array in my main class

The current code shows the way I have tried to call the constructor and initialise my array

Student Class:

     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); 
    }
}

Student info class:

    **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));         
    }
}**

Your Student class should be like this:

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);
    }
}

When you are calling greeting() , only the Student calling it, should greet. Not all the Student s.

And you can change your StudentInfo class to this:

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();
        }
    }
}

Hope it will work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM