Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Nested for loop

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Nested for loop

    So, I got the pattern right, but I was wondering if anybody else had a different approach to print out the same pattern using 2 nested for loops.
    When I try to include spaces in the inner loop to space the numbers it doesn't look the same. Any help?

    Pattern III
    ---------1
    -------2 1
    -----3 2 1
    ---4 3 2 1

    My code was:
      for(int i = 1; i <= 4; i++){
          System.out.println();
          for(int j = 4; j >= 1; j--){
              if(j <= i)System.out.print(j);
             else System.out.print(" ");
          }	
      }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Nested for loop

    Quote Originally Posted by wolf_fcknHaley33 View Post
    When I try to include spaces in the inner loop to space the numbers it doesn't look the same. Any help?
    Try highlighting it as code.

                1
              2 1
            3 2 1
          4 3 2 1
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Nested for loop

    If you would like to do it in another way, you can use String formatting. First build the string using an inner for loop, set the required width and print.

Similar Threads

  1. Nid help in nested loop !!
    By Spice in forum Loops & Control Statements
    Replies: 1
    Last Post: February 14th, 2012, 10:10 AM
  2. Nested loop if statement help
    By CSUTD in forum Loops & Control Statements
    Replies: 7
    Last Post: November 8th, 2011, 01:05 PM
  3. nested loop structure
    By captianjaax in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 21st, 2011, 04:00 PM
  4. The defitition of nested loop
    By silentbang in forum Java Theory & Questions
    Replies: 2
    Last Post: October 16th, 2010, 02:38 PM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM