我必须返回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));
嗯,而我可以提供一个答案,我敢肯定,我们有一个[XY问题(HTTPS ://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)在这里,你可以编辑问题并告诉我们“X”:)? – niceman
您最初将“最低”设置为最低值,然后将其设置为循环中的索引值。您需要分别跟踪索引和值。 –