Solved

Calculate the average rainfall over a period of years

Posted on 2009-07-01
2
2,891 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
Comment Utility
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
Comment Utility
Thank you very much!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Understanding @ResponseBody annotation 5 49
either24  challenge 19 83
modThree challenge 4 64
Handling json in url param 1 41
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now