java - 如何正确调用另一个类的构造函数,然后将其作为数组打印到控制台?
问题描述
我试图通过调用另一个类的构造函数并在我的主类中初始化一个数组来将 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));
}
}**
解决方案
你的学生类应该是这样的:
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();
}
}
}
希望它对你有用。
推荐阅读
- azure - libreswan 和 Azure VPN 网关之间的 Phase2 错误
- python - anaconda 提示符和命令提示符是否相同?
- r - 用于操作性田间实验设计的 R 函数 - 植物生态学
- python - 使用 BeautifulSoup 查找具有一个且只有一个属性的 html-tag
- python - ModuleNotFoundError:没有名为“apidjangowithjwt.emailservice”的模块
- html - 即使将边距设置为百分比,元素也会跳到父级之外
- c# - FedEx 跟踪 - 无法省略 ParentCredentials - 错误消息:“身份验证失败”,错误代码 1000
- algorithm - 如何编写算法来查找每周产品销售价格的利润
- python - 如何在 Bokeh 中制作带有时间序列的分类条形图?
- kubernetes - 如何在 Kubernetes 中使用 NodeSelector