Question
My Java program below runs, but the output for average and lowest display at zero. Can you tell me why this is happening so I
My Java program below runs, but the output for average and lowest display at zero.
Can you tell me why this is happening so I can fix it? I followed the script in my book, but it still doesn't work.
Thank you :)
public class Rainfall { public static void main(String[] args) { final int MONTHS_IN_YEAR = 12; double[] rainfallTotal = new double[MONTHS_IN_YEAR]; totalRainfall(rainfallTotal); averageRainfall(rainfallTotal); lowestRainfall(rainfallTotal); } public static void totalRainfall(double[] rainfallTotal) { double total = 0; Scanner keyboard = new Scanner(System.in); for(int index = 0; index < rainfallTotal.length; index++) { System.out.print("Please enter the amount of rainfall for month " + (index +1) + " : "); double userInput = keyboard.nextDouble(); total = total + userInput; } System.out.println("The total rainfall is " + total); } public static void lowestRainfall(double[] rainfallTotal) { double lowest = rainfallTotal[0]; for (int index = 0; index < rainfallTotal.length; index++) { if (rainfallTotal[index] < lowest); { lowest = rainfallTotal[index]; } } System.out.println("The lowest rainfall is " + lowest); } public static void averageRainfall(double[] rainfallTotal) { double total = 0; double average; for(int index = 0; index < rainfallTotal.length; index++) total = total + rainfallTotal[index]; average = total / rainfallTotal.length;
System.out.println("The average rainfall is " + average);
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started