Results 1 to 6 of 6
- 02-08-2011, 07:47 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Reduce redundancy in nested loop.
The program tells me to use triply nested loops. I know there's a way to modify for (int j = 1; j <= 1; j++){ to k, so I don't have to print k x5. But I ran out of ideas. Thanks in advance.Java Code:for (int i = 0; i <= 4; i++){ for (int j = 1; j <= 1; j++){ for (int k = 9; k >= 0; k--){ System.out.print(k); System.out.print(k); System.out.print(k); System.out.print(k); System.out.print(k); } System.out.println(); } }Last edited by masterboemi; 02-09-2011 at 12:14 AM.
- 02-08-2011, 11:00 PM #2
Hello and welcome!
Please use [code][/code] tags so we can read your code.
First, what is the output supposed to look like? Second, your second loop doesn't do anything since it starts at 1 and ends at 1.
- 02-08-2011, 11:50 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
you can get rid of a second loop since, like states above, it's unnecessary, and just have the third loop go from 0 - 4, printing k each time through.
- 02-09-2011, 12:19 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Yes I put the second loop in because the program won't run with only 2 loops.
And the output supposed to look like this 5 times:
99999888887777766666555554444433333222221111100000
- 02-09-2011, 12:24 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What happens if you have loop 1 do 0 - 4
the second loop does 9 - 0,
and the third loop is 0-4, and prints each k 1 time?
- 02-09-2011, 01:08 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Issue with nested for loop
By sunshine64 in forum New To JavaReplies: 5Last Post: 02-03-2011, 02:45 AM -
reduce redundancy
By hydride in forum New To JavaReplies: 4Last Post: 04-06-2010, 08:25 PM -
can some one help me with nested loop?
By keycoffee in forum New To JavaReplies: 10Last Post: 01-25-2010, 02:49 AM -
nested for loop question
By javabob in forum New To JavaReplies: 3Last Post: 05-20-2008, 11:00 PM -
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07-08-2007, 01:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks