整数不等于时不比较

Integer does not compare when not equals

我有一种方法可以通过将 student 数组和 topStudentIndex 作为参数来计算学生数组中的下一个最高分,这是通过 [=13= 的另一种方法计算的] 在我的主要。

下面是我的方法:

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);

您正在将 topStudentIndexst[i].getStudentIndex() 进行比较,但在打印语句中打印 i

要么

if ((st[i] != null) && (!(i == topStudentIndex)))

或打印

 System.out.println(topStudentIndex+ " compare with student index = "+ st[i].getStudentIndex());

弄清楚比较失败的原因。