gpt4 book ai didi

java - 将对象保存到数组中

转载 作者:行者123 更新时间:2023-12-01 07:50:15 25 4
gpt4 key购买 nike

我有两个类;类(class)和学生类(class)。 Student 类中的字段之一是类(class)。当我运行这个程序时,我无法在声明的数组中保存类(class)对象。下面的代码将在失败的地方提供更多信息:

package school;
public class Course
{
private String courseName;
private String courseGrade;
int counter;

public Course()
{
courseName="";
courseGrade="";
counter = 0;
}
public Course (String name,String grade)
{
courseName=name;
courseGrade=grade;
counter=0;
}
public String getCourseName()
{
return courseName;
}
public String getCourseGrade()
{
return courseGrade;
}
public void setCourse(String name,String grade)
{
courseName=name;
courseGrade=grade;
counter++;
}

@Override
public String toString ()
{
return String.format("%-10s %-10s", courseName,courseGrade);
}
}
package school;
public class Student
{
private String firstName;
private String lastName;
private int counter;
private Course [] myCourse;

public Student ()
{
firstName="";
lastName="";
myCourse =new Course[2];
for (int i=0;i<2;i++)
{
myCourse [i]= new Course ();
}
counter= 0;
}
public Student(String first,String last)
{
firstName=first;
lastName=last;
myCourse =new Course[2];
for (int i=0;i<2;i++)
{
myCourse [i]= new Course ();
}
counter= 0;
}
public Student(String first,String last, Course [] newCourse)
{
firstName=first;
lastName=last;
myCourse= new Course [2];
for (int i=0;i<2;i++)
{
myCourse [i]= new Course ();
}
counter++;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public int getNumberOfCourses()
{
return counter;
}
public void setStudent(String first,String last, Course [] newCourse)
{
firstName=first;
lastName=last;
myCourse= new Course [2];
for (int i=0;i<2;i++)
{
myCourse [i]= newCourse[i];
}
counter++;
}
public void setStudent(String first,String last)
{
firstName=first;
lastName=last;
}

public void registerCourses(Course newCourse)
{
for (int i=0;i<2;i++)
{
myCourse[i]=newCourse;
}
counter++;
}
public String toString()
{
String getString;
getString =(firstName+" "+lastName);

for(int i=0;i<2;i++)
{
getString=getString+String.format("%n %-10s",myCourse[i]);
}
return getString;
}
}
package school;

import java.util.*;
public class Test
{
static Scanner cin= new Scanner(System.in);
public static void main(String[] args)
{
String first,last,name,grade;

Student [] myStudent= new Student [2];
Course [] myCourse= new Course [2];
for (int i=0;i<2;i++)
{
myStudent[i] = new Student();
System.out.println("enter name:");
first=cin.next();
System.out.println("Last Name:");
last=cin.next();
// For loop within a for loop
for (int j=0;j<2;j++)
{
myCourse [j]= new Course ();
System.out.println("Course Name:");
name=cin.next();
System.out.println("Course Grade:");
grade=cin.next();
myCourse [j] = new Course(name,grade);
myStudent[i].registerCourses(myCourse [j]);
}
myStudent [i]=new Student(first, last, myCourse);
}
for (int i=0;i<2;i++)
{
System.out.println(myStudent[i]);
}
}
}

问题出在第二个 for 循环中,它无法保存我想要的类(class)对象。我希望程序输出类似于以下内容的内容:

Student One
MAT A
PHY B
BIO C
CHE A
LAN A+
HIS A
Student Two
COM C
Statistics A+
COM C
Mathematics D
Geography A
Biology C+
Student Three
Biology C
Mathematics D
LAN A+
COM B
Physics B
Physics B

但是我无法获得该输出。事实上我得到以下输出:

enter name:
Student
Last Name:
One
Course Name:
MAT
Course Grade:
A
Course Name:
COM
Course Grade:
C
enter name:
Student
Last Name:
Two
Course Name:
STA
Course Grade:
A
Course Name:
ENG
Course Grade:
B
Student One


Student Two

最佳答案

在循环结束时对同一索引执行 new 时,您正在销毁第一个 myStudent[i] 数据:

    for (int i=0;i<2;i++)
{
myStudent[i] = new Student();
<some code>
myStudent [i]=new Student(first, last, myCourse);

关于java - 将对象保存到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39147640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com