我必须返回 12 个数字数组中最低值的索引号。每次运行它时,我都会得到 12 作为结果。这是我的代码:
minRain = leastRain(months);
public static int leastRain (double[] mon4){
int lowest = (int) mon4[0];
for (int index=1; index<mon4.length; index++){
if (mon4[index]<lowest)
lowest = index;
}
return lowest;
}
System.out.println("The month with the lowest amount of rain is: " + (minRain + 1));
最佳答案
您犯了一个愚蠢的错误 - 您将索引分配给变量而不是数组值。这样做:
public static int leastRain (double[] mon4){
int lowest = 0;
for (int index=1; index<mon4.length; index++){
if (mon4[index]<mon4[lowest])
lowest = index;
}
return lowest;
}
关于java - 查找数组中最低值的索引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43813633/