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.
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.
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)));
}
}