我有两节课;课程和学生课程。 Student课程中的一个领域是课程。当我运行这个程序时,我无法在声明的数组中保存一个课程对象。下面的代码将为事情失败提供更多亮点:
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循环中,它无法保存我想要的课程对象。 我希望程序输出类似于以下内容的内容:
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
答案 0 :(得分:6)
在循环结束时对同一索引执行myStudent[i]
时,您正在销毁第一个new
数据:
for (int i=0;i<2;i++)
{
myStudent[i] = new Student();
<some code>
myStudent [i]=new Student(first, last, myCourse);
答案 1 :(得分:0)
由于你有一个未定义的课程数量,我建议使用ArrayList然后你做
另外,想想你在这做什么。
public void registerCourses(Course newCourse)
{
for (int i=0;i<2;i++)
{
myCourse[i]=newCourse;
}
counter++;
}
您正在添加一门课程,然后在我的阵列中为每门课程说添加我获得的新课程
您应该使用索引,或使用arraylist或linkedlist等。
E.g。
public void registerCourses(Course newCourse, int index)
{
myCourse[index]=newCourse;
}
或
public void registerCourses(Course newCourse)
{
myCourse.push(newCourse); // that is given myCourse is an ArrayList<Course>
}
答案 2 :(得分:0)
检查Student类中的registerCourses方法。它似乎在同一个索引上覆盖了你的结果。 您可以使用List执行此操作并继续添加。
这是我更新的学生班。
<tag>foo</tag> and <tag>bar</tag> are different from <tag>foo bar</tag>.
测试类:
package com.stack;
import java.util.ArrayList;
import java.util.List;
public class Student
{
private String firstName;
private String lastName;
private int counter;
private List<Course> myCourse;
public Student ()
{
firstName="";
lastName="";
myCourse =new ArrayList<Course>();
counter= 0;
}
public Student(String first,String last)
{
firstName=first;
lastName=last;
myCourse =new ArrayList<Course>();
counter= 0;
}
public Student(String first,String last, List<Course> newCourse)
{
firstName=first;
lastName=last;
myCourse= newCourse;
counter++;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public int getNumberOfCourses()
{
return counter;
}
public void setStudent(String first,String last, List<Course> newCourse)
{
firstName=first;
lastName=last;
myCourse= newCourse;
counter++;
}
public void setStudent(String first,String last)
{
firstName=first;
lastName=last;
}
public void registerCourses(Course newCourse)
{
myCourse.add(newCourse);
counter++;
}
public String toString()
{
String getString;
getString =(firstName+" "+lastName);
for(int i=0;i<2;i++)
{
getString=getString+String.format("%n %-10s",myCourse.get(i));
}
return getString;
}
}
注意:对于3名学生,您需要迭代循环,直到i <3(不是2)。