我怀疑在编写"最低分数"方法时我犯了一个错误(我或多或少否定了我的"最大分数"方法,因为它始终返回正确的值(数组中的最高分).但由于某种原因,我的最低分数方法要么只返回数组中的第一个元素或一些任意数字,甚至不返回数组.任何想法?
public static double highestScore(double[] scores)
{
double max = scores[0];
for (int i=0; i < scores.length; i++) {
if (scores[i] > max) {
max = scores[i];
}
}
return max;
}
public static double lowestScore(double[] scores) //possible problem some where in
{ //here?
double min = scores[0];
for (int i=0; i > scores.length; i++) {
if (scores[i] < min) {
min = scores[i];
}
}
return min;
}
Run Code Online (Sandbox Code Playgroud)
是的,问题在于lowestScore.你既倒<和>,但你还是应该遍历整个数组.在您的代码中,i > scores.length(0 > scores.length最初)求值为false,因此循环不会被执行并min始终等于scores[0].
更改
for (int i=0; i > scores.length; i++)
Run Code Online (Sandbox Code Playgroud)
至
for (int i=0; i < scores.length; i++)
Run Code Online (Sandbox Code Playgroud)