Results 1 to 7 of 7
  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Cool Using Nested Loops

    Display the following: Using loops
    1
    21
    321
    4321
    54321
    This is my attempt.
    Java Code:
    public class UsingLoops {
    	public static void main(String[] args) {
    		for (int line = 1; line <= 5; line++) {
        		for (int j = 1; j <= (-1 * line + 5); j++) {
            		System.out.print(" ");
        		}
    			System.out.print(line);
    			for (int j = 1; j >= (-1 * line + 3); j--) {
    				System.out.print(j + 3);
        		}
        		System.out.println();
    		}
    	}
    }
    This is the output that I get:
    1
    24
    343
    4432
    54321
    What am I doing wrong? How do I fix my code?
    It appears the space aren't showing when I created this thread. There is supposed to be 4 spaces before the first line, 3 spaces before the second line and so on.
    Last edited by son012189; 09-27-2011 at 12:31 AM.

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

    Default Re: Using Nested Loops

    For one, your second inner loop, the one that creates the numbers to be displayed:

    Java Code:
                for (int j = 1; j >= (-1 * line + 3); j--) {
                    System.out.print(j + 3);
                }
    needs to be a lot simpler. Think through how you'd do the logic on paper and you'll solve it.

  3. #3
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Using Nested Loops

    I'm still not sure how to get the program to work with a nested loop. However I do know how to get it work using only one loop.

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

  5. #5
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Using Nested Loops

    Nevermind, I finally got the code to work. Here's my solution.

    Java Code:
    public class UsingLoops {
    	public static void main(String[] args) {
    			for (int line = 1; line <= 5; line++) {
        			for (int j = 1; j <= (-1 * line + 5); j++) {
            		System.out.print(" ");
    				}
    				for (int j = line; j >= 1; j--) {
    				System.out.print(j);
        			}
        		System.out.println();
    			}
    	}
    }

  6. #6
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Using Nested Loops

    Nitpicky, but you might find it useful to consider that -1 * line can instead be rendered -line.

  7. #7
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

Similar Threads

  1. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  2. Nested Loops
    By joemama in forum New To Java
    Replies: 1
    Last Post: 01-01-2011, 09:17 PM
  3. Nested Loops
    By candygirl198827 in forum New To Java
    Replies: 38
    Last Post: 12-01-2010, 06:03 AM
  4. Nested for loops
    By luke in forum New To Java
    Replies: 23
    Last Post: 10-21-2010, 02:49 AM
  5. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 08:47 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
  •