У меня есть метод, который вычисляет следующий верхний балл в массиве студентов, беря в нем массив 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());
чтобы понять, почему сравнение не удается.
st[i].getStudentIndex()
, а неi
...