对象数组 -java-
array of object -java-
我有一个练习,我写了一个 class CourseManager5 来存储给定的学生对象列表
class。每个学生都有一个ID,姓名,分数。 class 允许用户添加一个
学生,并显示学生的数据。这是 UML 图:
enter image description here
这些方法是:
- CourseManager5: 一个初始化属性并创建大小为 100 的学生数组的构造函数。
- getNStudents : returns当前学生人数。
- addStudent:将具有给定对象的学生添加到列表中。如果课程已满,
它打印错误消息:“错误:课程已满”。
- displayStudent:显示第i个学生的所有数据。
编写一个名为 TestCourseManager5 的 main class,其中包含一个 main 方法,它将
执行以下操作:
- 它创建一个 CourseManager5 对象。
- 然后,它通过读取用户的 ID、姓名和分数来添加 3 个学生。
- 然后,显示class中的所有学生。
我写了程序..但是我在使用方法displayStudent时遇到了问题..我不明白为什么!!
还有一件事:方法 addStudent 正确吗??
这是我的程序:
import java.util.Scanner;
public class TestCourseManager5 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
courseManager5 cm = new courseManager5();
int num =0;
for(int i =0; i < 3;i++){
System.out.println("Please enter the ID, name, and score of student "+num+":");
int id = s.nextInt();
String name = s.next();
double score = s.nextDouble();
student su = new student(id, name, score);
cm.addStudent(su);
num++;}
System.out.println("Student are: ");
for(int i=0;i<cm.getNStudent();i++){
cm.displayStudent(i);
}}}
public class student {
private int id;
private String name;
private double score;
public student(int id, String name, double score){
this.id = id;
this.name = name;
this.score = score;}
public int getId(){
return id;}
public String getName(){
return name;}
public double getScore(){
return score;}}
public class courseManager5 {
private student []students;
private int nStudent;
public static int MAX_SIZE = 100;
public courseManager5(){
students = new student[MAX_SIZE];
nStudent = 0;}
/addStudent 将具有给定对象的学生添加到列表中。如果课程已满,
它打印错误消息:“错误:课程已满”我的方法是否正确?/
public void addStudent (student newStudent){
if(nStudent < MAX_SIZE)
nStudent++;
else
System.out.println("ERROR: COURSE IS FULL");}
public int getNStudent(){
return nStudent;}
public void displayStudent(int i){// my problem here !
System.out.println(students[i].getId()+", "+students[i].getName()+", "+students[i].getScore());
}
}
在您的 addStudent
方法中,您没有将传入的学生添加到数组中。试试这个:
students[nStudent] = newStudent;
您的问题出在 addStudent() 中。您从未将学生添加到数组中。因此,这导致 displayStudent() 中为空。试试这个:
public void addStudent (student newStudent){
if(nStudent < MAX_SIZE)
students[nStudent++] = newStudent;
else
System.out.println("ERROR: COURSE IS FULL");}
我有一个练习,我写了一个 class CourseManager5 来存储给定的学生对象列表 class。每个学生都有一个ID,姓名,分数。 class 允许用户添加一个 学生,并显示学生的数据。这是 UML 图: enter image description here 这些方法是:
- CourseManager5: 一个初始化属性并创建大小为 100 的学生数组的构造函数。
- getNStudents : returns当前学生人数。
- addStudent:将具有给定对象的学生添加到列表中。如果课程已满, 它打印错误消息:“错误:课程已满”。
- displayStudent:显示第i个学生的所有数据。
编写一个名为 TestCourseManager5 的 main class,其中包含一个 main 方法,它将
执行以下操作:
- 它创建一个 CourseManager5 对象。
- 然后,它通过读取用户的 ID、姓名和分数来添加 3 个学生。
- 然后,显示class中的所有学生。
我写了程序..但是我在使用方法displayStudent时遇到了问题..我不明白为什么!!
还有一件事:方法 addStudent 正确吗??
这是我的程序:
import java.util.Scanner;
public class TestCourseManager5 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
courseManager5 cm = new courseManager5();
int num =0;
for(int i =0; i < 3;i++){
System.out.println("Please enter the ID, name, and score of student "+num+":");
int id = s.nextInt();
String name = s.next();
double score = s.nextDouble();
student su = new student(id, name, score);
cm.addStudent(su);
num++;}
System.out.println("Student are: ");
for(int i=0;i<cm.getNStudent();i++){
cm.displayStudent(i);
}}}
public class student {
private int id;
private String name;
private double score;
public student(int id, String name, double score){
this.id = id;
this.name = name;
this.score = score;}
public int getId(){
return id;}
public String getName(){
return name;}
public double getScore(){
return score;}}
public class courseManager5 {
private student []students;
private int nStudent;
public static int MAX_SIZE = 100;
public courseManager5(){
students = new student[MAX_SIZE];
nStudent = 0;}
/addStudent 将具有给定对象的学生添加到列表中。如果课程已满, 它打印错误消息:“错误:课程已满”我的方法是否正确?/
public void addStudent (student newStudent){
if(nStudent < MAX_SIZE)
nStudent++;
else
System.out.println("ERROR: COURSE IS FULL");}
public int getNStudent(){
return nStudent;}
public void displayStudent(int i){// my problem here !
System.out.println(students[i].getId()+", "+students[i].getName()+", "+students[i].getScore());
}
}
在您的 addStudent
方法中,您没有将传入的学生添加到数组中。试试这个:
students[nStudent] = newStudent;
您的问题出在 addStudent() 中。您从未将学生添加到数组中。因此,这导致 displayStudent() 中为空。试试这个:
public void addStudent (student newStudent){
if(nStudent < MAX_SIZE)
students[nStudent++] = newStudent;
else
System.out.println("ERROR: COURSE IS FULL");}