Java 整数不等于时不进行比较
我有一种方法,通过将Java 整数不等于时不进行比较,java,arrays,netbeans,integer,compare,Java,Arrays,Netbeans,Integer,Compare,我有一种方法,通过将student数组和topstudentidex作为参数来计算学生数组中的下一个最高分数,这是通过我的main中的另一种方法对topstudentidex计算的 以下是我的方法: public static int calNextHighestOverallStudentIndex(Student[] st, int topStudentIndex) { double nextHighestOverall= 0; int secondHighestStude
student
数组和topstudentidex
作为参数来计算学生数组中的下一个最高分数,这是通过我的main中的另一种方法对topstudentidex
计算的
以下是我的方法:
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 ;
}
我试过使用=代码>和我当前的检查方式,但没有效果
if ((st[i] != null) && (!(st[i].getStudentIndex() == topStudentIndex))) {
System.out.println(topStudentIndex+ " compare with i = "+i);
您正在将topstudentidex
与st[i].getStudentIndex()进行比较,但在print语句中打印i
或者
if ((st[i] != null) && (!(i == topStudentIndex)))
或打印
System.out.println(topStudentIndex+ " compare with student index = "+ st[i].getStudentIndex());
了解比较失败的原因。您正在比较st[i]。getStudentIndex()
,而不是i
…谢谢@Aniket。这种比较现在奏效了。我遇到了另一个问题,我的for循环没有循环。我将在另一个帖子中发布这个问题。再次感谢你
System.out.println(topStudentIndex+ " compare with student index = "+ st[i].getStudentIndex());