gpt4 book ai didi

java - 包含 if 语句的 for 循环不会再次循环

转载 作者:行者123 更新时间:2023-11-30 08:54:41 36 4
gpt4 key购买 nike

下面是一个方法,它接受 student 数组和 topStudentIndex 的值,该值由之前的另一种方法计算得出。

我的数组中有多个对象。然而,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 ;
}

最佳答案

看起来像

double studentOverall = st[nextHighestStudentIndex ].getOverall();

应该是

double studentOverall = st[i].getOverall();

因为你想检查数组中的所有学生,而不仅仅是第一个(st[nextHighestStudentIndex ] 将始终返回第一个学生,因为你初始化了 nextHighestStudentIndex到 0)。

关于java - 包含 if 语句的 for 循环不会再次循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29326857/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com