Solved

Calculate the average rainfall over a period of years

Posted on 2009-07-01
2
2,917 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the compatible hibernate version for spring 4.3.1 10 92
matchUp  challenge 9 108
endX challenge 2 78
ejb on wildfly 5 20
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 …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

770 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