Results 1 to 4 of 4
Thread: Problem with nested for loops
- 04-07-2012, 06:04 PM #1
Member
- Join Date
- Feb 2012
- Location
- Phoenix, AZ
- Posts
- 26
- Rep Power
- 0
Problem with nested for loops
Hello,
Could you please help me figure out how to solve this problem?
Write nested for loops that produce the following output:
000111222333444555666777888999
000111222333444555666777888999
000111222333444555666777888999
This is what I have, but it's wrong.
Java Code:for(int i = 1; i <= 3; i++){ for(int j = 0; j <= 9; j++){ for(int k = 1; k <= 3; k++){ System.out.println(j); } } }
-
Re: Problem with nested for loops
How is your code wrong? What is it doing incorrectly?
And have you ever used System.out.print(...)? Do you know the difference between the behavior of this method and the println(...) method?
- 04-07-2012, 10:07 PM #3
Member
- Join Date
- Feb 2012
- Location
- Phoenix, AZ
- Posts
- 26
- Rep Power
- 0
Re: Problem with nested for loops
The orginial code was printing one character per line. Didn't know there was a print method! It now works as written below.
Java Code:for(int i = 1; i <= 3; i++){ for(int j = 0; j <= 9; j++){ for(int k = 1; k <= 3; k++){ System.out.print(j); if(j==9 && k==3) System.out.println(""); } } }
-
Re: Problem with nested for loops
You're welcome, but you don't even need the if block, simply move the println outside of the inner loops.
Similar Threads
-
Nested Loops Help!
By spendogw in forum New To JavaReplies: 2Last Post: 03-16-2012, 05:20 AM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 05:02 AM -
Nested for loops
By luke in forum New To JavaReplies: 23Last Post: 10-21-2010, 03:49 AM -
nested for loops
By Implode in forum New To JavaReplies: 4Last Post: 09-01-2009, 09:47 AM -
Nested Loops
By ks1615 in forum New To JavaReplies: 4Last Post: 02-18-2009, 03:48 AM
Bookmarks