我有两个名为Student and Subject
的课程import java.util.Vector;
public class Student
{
int registrationNumber;
String firstName;
String familyName;
Vector<Subject> sub;
public Student(int registrationNumber,String firstName, String familyName, Subject sub) {
this.registrationNumber = registrationNumber;
this.firstName = firstName;
this.familyName = familyName;
this.sub = new Vector<Subject>();
}
public Vector<Subject> getSub(){
return sub;
}
}
和
int subjectNumber;
Vector<Double> homeworkMark;
Vector<Double> examinationMark;
public Subject(int subjectNumber) {
this.subjectNumber = subjectNumber;
this.homeworkMark = new Vector<Double>();
this.examinationMark = new Vector<Double>();
}
public double homeworkAverage() {
double homeworkAddition = 0;
for(Subject y: getSub()){
for (double s: homeworkMark) {
homeworkAddition += s;
}
}
return homeworkAddition;
}
public double examinationAverage() {
double examAddition = 0;
for(Subject y: getSub()){
for (double s: examinationMark) {
examAddition += s;
}
}
return examAddition;
}
我正在尝试使用名为getSub()
的getter方法在用户输入的所有主题中添加Homeworks和考试标记,这些主题存储在 sub { {1}}。
我的问题出现在这个循环中(也在类似的Vector
方法
examinationAverage()
我尝试创建一个学生实例,但它告诉我我没有设置参数,而且我无法继续......
任何想法,
先谢谢。
答案 0 :(得分:0)
对我来说,他们有两件奇怪的事情:
Subject
构造函数中传递Student
参数但不使用它Student
类中,但是既不在给定的Student实例上也不以静态方式从Subject
访问。答案 1 :(得分:0)
我猜你是用一个初始的Subject参数“sub”来实例化你的Student类?在这种情况下,您缺少将“sub”参数添加到Vector:
public Student(int registrationNumber,String firstName, String familyName, Subject sub) {
this.registrationNumber = registrationNumber;
this.firstName = firstName;
this.familyName = familyName;
this.sub = new Vector<Subject>();
this.sub.add(sub); // <-- here
}
否则sub参数被丢弃......
答案 2 :(得分:0)
这里有几个问题:
为了调用给定对象的方法,你必须亲自拥有该对象,所以在你的情况下是这样的:
public double homeworkAverage() {
double homeworkAddition = 0;
for(Subject y: student.getSub()){ //....
根据您发布的内容,您无法意识到homeworkAverage
的声明/使用位置,但您可以将其设置为静态并提供Student
参数:
public static double homeworkAverage(Student student) {
double homeworkAddition = 0;
for (Subject y : student.getSub()) { ....
我希望它有一些亮点