# Help with Nested Loops

• October 23rd, 2010, 02:02 PM
Plural
Help with Nested Loops
Assignment:
Average Rainfall
Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. First 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 should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.
Input validation: Do not accept a number less than 1 for the number of years. Do not accept negative numbers for the monthly rainfall.

Code java:

```/* * To change this template, choose Tools | Templates * and open the template in the editor. */   package averagerainfall; import java.util.Scanner;   /** * * @author uuuuuuu */ public class Main {   /** * @param args the command line arguments */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in);   int years; int months; double rainfall = 0; double totalRainfall = 0.0;     System.out.println("Enter the number of years:"); years = keyboard.nextInt();   for(int count = 1; count<= years;count++) { for(months = 1; months<=12;months++) { System.out.println("Enter inches of rainfall for month " + months); rainfall = keyboard.nextDouble(); totalRainfall += rainfall;; } System.out.println("Number of months: " + (months)); System.out.println("Total inches of rainfall: " + totalRainfall); System.out.println("Average rainfall per month: " + (totalRainfall/(years * months))); } } }```

In the code, where do I put [rainfall>0]?

When I enter [1] for years, the output for number of months is [13], why isn't it [12], how do I fix it?

When I put [2] for years, why doesn't the number of months accumulate; it should be [24] months not [12] and [12] (in my case, it is [13] and [13] because of coding problems.'

I'll fix it the output decimal format into (##0.00) afterward.
• October 23rd, 2010, 02:47 PM
javapenguin
Re: Help with Nested Loops
It's possible that your value for months is lost after that for loop ends. It either will print out 12 when you say
System.out.println("Number of months" + months); or it will claim it hasn't been initialized. I think it'll print out 12.

Perhaps you should try putting the three lines of code within the brackets of the second for loop.

Also, you only told it to go to twelve months per year. When the year ends, it starts over at month 1.

1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12.

I'm not sure about the format, but maybe %2d might work.

No clue why it would be 13 months.

Try

for (int month = 1; month < 13; m++)

instead of the the <=12 to see if that was causing it.

Also, perhaps, maybe, your final 3 lines should be outside all of the for loops.

Also, you could simply calculate it to do this:

System.out.println("Number of months" + (years*12));

However, I think that it's possibly still incrementing somehow when it gets to your last 3 statements.
• October 23rd, 2010, 03:08 PM
Plural
Re: Help with Nested Loops
Quote:

Originally Posted by javapenguin
It's possible that your value for months is lost after that for loop ends. It either will print out 12 when you say
System.out.println("Number of months" + months); or it will claim it hasn't been initialized. I think it'll print out 12.

Perhaps you should try putting the three lines of code within the brackets of the second for loop.

Also, you only told it to go to twelve months per year. When the year ends, it starts over at month 1.

1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12.

I'm not sure about the format, but maybe %2d might work.

No clue why it would be 13 months.

Try

for (int month = 1; month < 13; m++)

instead of the the <=12 to see if that was causing it.

The assignment says that it should loop 12 times, once for each month. Thus, it should be months<=12.
I think I'll have to put an "if-statement" in the inner loop; but what do I put as the boolean for it?
The cause of it to appear as 13 months is because of the "months++"; an "if-statement" would resolve it but I'm not sure what boolean condition.
And did what you told me, I put the 3 statements in the inner loop.

As for the decimal format, I'll have to put
import java.text.DecimalFormat;
at the top of the code (under package) and then in the code:
DecimalFormat fmt = new DecimalFormat("##0.00");
and inside the output, I'll just have to add in
fmt.format
in front of the number that I want to use the format on.
• October 23rd, 2010, 03:31 PM
javapenguin
Re: Help with Nested Loops
Code java:

```import java.util.Scanner;   /** * * @author uuuuuuu */ public class Main {   /** * @param args the command line arguments */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in);   int years; int months; double rainfall = 0; double totalRainfall = 0.0;     System.out.println("Enter the number of years:"); years = keyboard.nextInt();   for(int count = 1; count<= years;count++) { for(months = 1; months<=12;months++) { System.out.println("Enter inches of rainfall for month " + months); rainfall = keyboard.nextDouble(); totalRainfall += rainfall;; }   } System.out.println("Number of months: " + (years*12)); System.out.println("Total inches of rainfall: " + totalRainfall); System.out.println("Average rainfall per month: " + (totalRainfall/(years * 12))); } }```