我有一种方法可以通过将student
数组和topStudentIndex
作为参数来计算学生数组中的下一个最高分,这是通过topStudentIndex
in的另一种方法计算的我的主要。
以下是我的方法:
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 ;
}
我接受顶尖学生的索引位置,并检查阵列位置是否为空&amp;&amp;相同的指数。如果没有,检查将继续。
但是,输出显示索引位置的比较不起作用。
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
我尝试使用!=
和我目前的检查方式,但无济于事。
答案 0 :(得分:1)
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());
明白为什么比较失败。