Solved

Calculate the average rainfall over a period of years

Posted on 2009-07-01
2
3,018 Views
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++)
		{
			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

0
Comment
Question by:gkilgore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
humanonomics earned 500 total points
ID: 24760223
Try this
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;
        double bigTotal=0;
        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];
			}
			bigTotal += total;
			System.out.println("The total rainfall for the year # " + y + "is "+ total);
									
			average = total/12;
			System.out.println("The average rainfall of the year # " + y + " is "+ average);
					
		}
		System.out.println("The total rainfall of all the years is "+ bigTotal);
		average = bigTotal/(years*12);
		System.out.println("The average rainfall of all the years is "+ average);
 
	}
}

Open in new window

0
 

Author Closing Comment

by:gkilgore
ID: 31599034
Thank you very much!
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question