Results 1 to 6 of 6
  1. #1
    masterboemi is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Reduce redundancy in nested loop.

    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();
       }
    }
    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.
    Last edited by masterboemi; 02-09-2011 at 12:14 AM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  4. #4
    masterboemi is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    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

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  6. #6
    masterboemi is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Oh yeah that would work just fine. With that, now I have a print instead of 5 prints. Thanks a bunch.

Similar Threads

  1. Issue with nested for loop
    By sunshine64 in forum New To Java
    Replies: 5
    Last Post: 02-03-2011, 02:45 AM
  2. reduce redundancy
    By hydride in forum New To Java
    Replies: 4
    Last Post: 04-06-2010, 08:25 PM
  3. can some one help me with nested loop?
    By keycoffee in forum New To Java
    Replies: 10
    Last Post: 01-25-2010, 02:49 AM
  4. nested for loop question
    By javabob in forum New To Java
    Replies: 3
    Last Post: 05-20-2008, 11:00 PM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •