Results 1 to 4 of 4
  1. #1
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  3. #3
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default 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("");
            }
        }
    }
    Thanks!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Nested Loops Help!
    By spendogw in forum New To Java
    Replies: 2
    Last Post: 03-16-2012, 05:20 AM
  2. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  3. Nested for loops
    By luke in forum New To Java
    Replies: 23
    Last Post: 10-21-2010, 03:49 AM
  4. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 09:47 AM
  5. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 03:48 AM

Posting Permissions

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