我有一种方法,通过将student
数组和topStudentIndex
作为参数来计算学生数组中的下一个最高分数,这是我主要针对topStudentIndex
的另一种方法计算的。
以下是我的方法:
public static int calNextHighestOverallStudentIndex(Student[] st, int topStudentIndex) {
double nextHighestOverall= 0;
int secondHighestStudentIndex = 0;
for (int i = 0; i < st.length; i++) {
if ((st[i] != null) && (!(st[i].getStudentIndex() == topStudentIndex))) {
System.out.println(topStudentIndex+ " compare with i = "+i);
double currentOverall= st[i].getOverall();
if (currentOverall> nextHighestOverall) {
nextHighestOverall= currentOverall;
secondHighestStudentIndex = i;
}
}
}
return secondHighestStudentIndex ;
}
我取状元的索引位置,并检查数组位置是否为空&;相同的索引。如果没有,将继续进行检查。
然而,结果表明,指数位置的比较不起作用。
1 compare with i = 0
1 compare with i = 1
1 compare with i = 2
1 compare with i = 3
1 compare with i = 4
1 compare with i = 5
1 compare with i = 6
我尝试过使用!=
和我目前的检查方式,但都没有用。
if ((st[i] != null) && (!(st[i].getStudentIndex() == topStudentIndex))) {
System.out.println(topStudentIndex+ " compare with i = "+i);
您正在比较topStudentIndex
和st[i].getStudentIndex()
,但在打印语句中打印i
。
要么做
if ((st[i] != null) && (!(i == topStudentIndex)))
或打印
System.out.println(topStudentIndex+ " compare with student index = "+ st[i].getStudentIndex());
以弄清楚为什么比较失败。