Java 包含if语句的For循环不会再次循环
下面是一种方法,它接受由另一种方法计算的值的Java 包含if语句的For循环不会再次循环,java,arrays,for-loop,netbeans,Java,Arrays,For Loop,Netbeans,下面是一种方法,它接受由另一种方法计算的值的student数组和topstudentidex 我的数组中有多个对象。然而,for循环只能循环一次,它立即返回第一个值 我不知道为什么for循环停止,即使我的st.length大于 public static int computeNextHighestOverallStudentIndex(Student[] st, int topStudentIndex) { double nextHighestOverall= 0; int
student
数组和topstudentidex
我的数组中有多个对象。然而,for循环只能循环一次,它立即返回第一个值
我不知道为什么for循环停止,即使我的st.length
大于
public static int computeNextHighestOverallStudentIndex(Student[] st, int topStudentIndex) {
double nextHighestOverall= 0;
int nextHighestStudentIndex = 0;
for (int i = 0; i < st.length; i++) {
if ((st[i] != null) && (!(i == topStudentIndex))){
double studentOverall = st[nextHighestStudentIndex ].getOverall();
if (studentOverall > nextHighestOverall) {
nextHighestOverall= studentOverall;
nextHighestStudentIndex = i;
}
}
}
return nextHighestStudentIndex ;
}
public static int computenexthighestorallstudentindex(Student[]st,int topstudenindex){
双下一个总值=0;
int nextHighestStudentIndex=0;
对于(int i=0;i下一个总体){
nextHighestOverall=学生总体;
下一个最高学生指数=i;
}
}
}
返回下一个最优秀的学生索引;
}
它看起来像
double studentOverall = st[nextHighestStudentIndex ].getOverall();
应该是
double studentOverall = st[i].getOverall();
由于您希望检查数组中的所有学生,而不仅仅是第一个学生(
st[nextHighestStudentIndex]
将始终返回第一个学生,因为您将nexthighestudentindex
初始化为0)。您希望使用st[i](而不是st[nexthighestudentindex])遍历整个数组。谢谢@Eran,改变后,它工作了。我马上就接受你的回答,等待计时器过期