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
Solved

Calculate the average rainfall over a period of years

Posted on 2009-07-01
2
2,963 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
bunnyEars2 challenge 6 148
bitbucket vs gitbucket 3 102
more than one jdk and one jre 1 50
mysql jsp example issue 32 50
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 …
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.

829 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