We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Calculate the average rainfall over a period of years

gkilgore asked
Medium Priority
Last Modified: 2013-11-24
The attached program has been written using nested for statements.  The program asks the users the enter the number of years and the inches of rain for each month.  Lastly provide the average inches of rain for the entire period.  

My program is about 90% completed, I'm having problem with where to add the for statement needed to display the average rainfall per month for the entire period?
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Rain1
	public static void main(String[] args)
	Scanner in=new Scanner(System.in);
 	int years;
	int month=12;
 	double total=0;
	double average;
	 double months[];
	String input;
	months=new double[12];
	DecimalFormat dollar = new DecimalFormat("#,##0.00");
		input = JOptionPane.showInputDialog("Number of years? "); 
		years = Integer.parseInt(input);
		for(int y=1; y<=years; y++)
			System.out.println("Enter the rainfall (in inches for year #"+y+":");
		for(int n=1; n<=month; n++ )
		System.out.println("Enter the rainfall (in inches) for month #"+n+":");
		months[n-1] = in.nextInt();
		total = 0;
		for(int i=0; i<12;i++)
			System.out.println("The total rainfall for the year is "+ total);
			average = total/12;
			System.out.println("The average rainfall of the year is "+ average);

Open in new window

Watch Question

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview


Thank you very much!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.