【问题标题】:Integer does not compare when not equals不等于时整数不比较
【发布时间】:2015-03-29 07:28:19
【问题描述】:

我有一种方法可以通过传入studentarray 和topStudentIndexas 参数来计算一组学生中的下一个最高分,这是由我主要的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

我尝试过使用!=和我目前的检查方式,但无济于事。

【问题讨论】:

  • 你在比较st[i].getStudentIndex(),而不是i...

标签: java arrays netbeans integer compare


【解决方案1】:
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());

明确比较失败的原因。

【讨论】:

  • 谢谢@Aniket。比较现在奏效了。我遇到了另一个问题,我的 for 循环没有循环。我将在另一个线程中发布问题。再次感谢您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
相关资源
最近更新 更多