I have to return the index number of the lowest value in an array of 12 numbers. I keep getting 12 as the result every time I run it. Here is my code:
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));
lowestto the lowest value initially and then setting it to the index value in the loop. You need to track the index and value separately.