I have a method that calculates the minimum and maximum amount of rainfall from a user input array of months. My problem is that my method returns the quantity of min and max and I need it to return the month that had the highest or lowest amount of rainfall. I am not sure how to compare my min and max methods to the months array. I imagine if I could say the min was found at index 5 then have a way to pull index 5 of the month array as well.
I have tried multiple types of for loops and nested for loops but can not seem to get the correct month.
public static void main(String[] args) {
String[] months = {"January", "February", "March","April","May","June","July","August","September","October","November","December"};
double[] rainfall = new double[months.length];
Rainfall amazon = new Rainfall();
//scanner to get user input
Scanner scanner = new Scanner(System.in);
//for loop to iterate through the months to get user input of rainfall each month
for(int i = 0; i<rainfall.length; i++) {
System.out.println("How much rainfall did you recieve, in inches, for the month of: " + months[i]);
rainfall[i] = scanner.nextDouble();
//while statement to reject negative numbers
while(rainfall[i] < 0) {
System.out.println("You can not enter a negative number");
System.out.println("How much rainfall did you recieve for the month of: " + months[i]);
rainfall[i] = scanner.nextDouble();
}
}
System.out.println("total rainfall: " + amazon.getTotalRainfall(rainfall) +
" average Rainfall: " + amazon.getAverageRainfall(rainfall) +
" min: " + amazon.getMinRainfall(rainfall) +
" max: " + amazon.getMaxRainfall(rainfall));
}
public double getMaxRainfall(double[] rainfall) {
double max = rainfall[0];
for(int i = 1; i < rainfall.length; i++)
if(rainfall[i] > max) {
max = rainfall[i];
}
return max;
}
public double getMinRainfall(double[] rainfall) {
double min = rainfall[0];
for(int i = 1; i < rainfall.length; i++)
if(rainfall[i] < min) {
min = rainfall[i];
}
return min;
}
I need to know the month that had the highest and lowest amount of rainfall