Link to home
Start Free TrialLog in
Avatar of gkilgore
gkilgore

asked on

Calculate the average rainfall over a period of years

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++)
		{
			total=total+months[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

ASKER CERTIFIED SOLUTION
Avatar of humanonomics
humanonomics
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gkilgore
gkilgore

ASKER

Thank you very much!