因此,我试图将数组作为参数传递给类,但当我打印到屏幕时,它仅显示最新的对象输入
for (int j=0;j<5;j++) {
do {
System.out.print("Enter Test Score " +(j+1)+" between 0-100 : ");
score = sc.nextInt();
sc.nextLine();
}while(score<0||score>100);
ts[j] = score;
}
s.setTestScore(ts);
System.out.println("");
当我传递名称和ID时,它可以使用完全相同的系统s.set(nameofmethod)
void setTestScore(int[] test) {
this.test = test;
}
int[] getTests() {
return test;
}
然后我将测试传递给一种方法进行计算,它只会打印出创建的每个对象的通过/失败,无论得分如何
void calculateResult() {
int sum = 0;
for(int i = 0; i < test.length; i++){
sum += test[i];
}
sum = sum/numTests;
if(sum<40) {
grade = "Fail";
}
else {
grade = "Pass";
}
System.out.println(grade);
}
答案 0 :(得分:0)
我认为您或者在为每个学生输入测试成绩后需要运行calculateResult,或者您需要某种数组的哈希值来跟踪哪个测试成绩数组属于哪个学生。由于您只有一个数组test[]
,因此,学生ID 5的分数将被学生ID 77的分数所覆盖。您可以尝试执行以下操作:
for (int j=0;j<5;j++) {
do {
System.out.print("Enter Test Score " +(j+1)+" between 0-100 : ");
score = sc.nextInt();
sc.nextLine();
}while(score<0||score>100);
ts[j] = score;
}
s.setTestScore(ts);
s.calculateResult();
System.out.println("");