We help IT Professionals succeed at work.

Average Rain Fall

1,198 Views
Last Modified: 2013-12-14
im trying to write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. Basically the program should ask for the number of years. The Outer Loop will iterate once for each year. The Inner Loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program shold display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.

Im having a hard time starting the code and that is why i have not posted anything.
Comment
Watch Question

a_b
Top Expert 2009

Commented:
To help you get started, nested loops -

for(int i=0;i<noOfYrs;i++)
{
     for(int j=0;,j<12;j++)
     {
           // Do all the processing here
     }
}

Author

Commented:
Thanks this helped a bit. Are you able to post the full code so I can compare to what have. I have a feeling I'm way off. My teacher doesn't do a well job at explaining much.
a_b
Top Expert 2009

Commented:
@AgentC4: Why don't you post your code so that we can have a look at what is wrong, instead of you copying the stuff off the website?

Author

Commented:
Ok ill post my code as soon as I'm finished with my exam. And no one said I'm just copingy and calling it a day. I've been doing over twenty problems because I've been on medical leave and my teachers notes online are probably better starting a fire rather than teach.

Author

Commented:
Oh no please don't think I'm accusing you or anyone I just don't want you guys to think I'm coming here for an easy outcome. I do need this stuff explained because my teacher isn't doing a great job at it. Ill post what I have soon after I leave the doctors office. And thanks in advance.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
This is what i have so far, i cant figure out which LOOP choice to go with.
public class AverageRainFall
{
    public static void main(String [] args)
    {
        double years;
        double months=12;
        double total=0;
        double average;
        double months[];
        double grandTotal=0;
        months = new double[12];
        String input;
 
        Scanner keyboard = new Scanner(System.in);
 
        DecimalFormat dollar = new DecimalFormat("##0.00");
 
        input = JOptionPane.showInputDialog("Please enter number of years? "); 
		
        years = Integer.parseInt(input);

Open in new window

Author

Commented:
sorry please use this for the decimal
DecimalFormat Rain = new DecimalFormat("##0.00");

Open in new window

Author

Commented:
My teacher didnt do  a well job at explaining the differences between WHILE, DO-WHILE, and FOR  nad how they work and thats where im pretty much stuck
OOps soory, I thought he is the same other guy, who had posted incomplete code in another question, their problem questions are pretty much same @ Venabili

https://www.experts-exchange.com/Programming/Languages/Java/J2EE/JavaMail/Q_24538353.html
Go step by step, first loop over the years initially and then loop over the months

Author

Commented:

public class AverageRainFall
{
    public static void main(String [] args)
    {
        double years;
        double months=12;
        double total=0;
        double average;
        double months[];
        double grandTotal=0;
        months = new double[12];
        String input;
 
        Scanner keyboard = new Scanner(System.in);
 
        DecimalFormat dollar = new DecimalFormat("##0.00");
 
        input = JOptionPane.showInputDialog("Please enter number of years? "); 
                
        years = Integer.parseInt(input);
 
        for(int y = 1; y <= years; y++) 
        {
           System.out.println("Please enter rainfall using inches for         year #: " + y +);
         
        }

Open in new window

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.