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 8 of 8

Thread: Using Nested Loops

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Lightbulb Using Nested Loops

    Display the following: Using loops
        1
       21
      321
     4321
    54321
    This is my attempt.

    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 helloworld922; September 27th, 2011 at 07:00 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Using Nested Loops

    What am I doing wrong? How do I fix my code?
    Your code is not printing the biggest number down to 1 on each line. Compute the largest number for the line and decrement it by 1 until you get to 1.
    It looks like your leading spaces are ok.

  3. The Following User Says Thank You to Norm For This Useful Post:

    m2msucks (October 1st, 2011)

  4. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Using Nested Loops

    Ok thanks. I managed to fix my code and get the right output. Here it is.
    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();
    		}
    	}
    }

  5. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Using Nested Loops

    this is my answer....
    try this....

    ...
    Last edited by copeg; November 5th, 2011 at 10:53 AM.

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Using Nested Loops

    Please wrap your posted code in code tags to preserve formatting.

  7. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,233
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Using Nested Loops

    Quote Originally Posted by ravindu
    this is my answer....
    Ravindu, please read the forum rules and the following link: The Problem with Spoon-feeding

  8. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    My Mood
    Starving
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using Nested Loops

    Quote Originally Posted by copeg View Post
    Ravindu, please read the forum rules and the following link: The Problem with Spoon-feeding
    So tell me, when you first start learning something at school or university, do they expect you to learn everything on your own, or do they help you solve things ?

    I understand your point about spoon feeding, but the guy is asking for help, don't u think he can sit and think if he had to ? and if he knew that he needs help, then he would ask for it ? I don't understand why the strict rules about not posting solutions...you always learn ...even if u took someone else's code and studied it, you would still learn, I'm sure that you can write more than one piece of code to do the exact same thing. so why not post a solution, and let the person asking for help, get the idea, and then try to recode it. Or perhaps if you really want, maybe u can use by pesudo code, instead of complete code, which in a way makes them learn the syntax better.

  9. #8
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,233
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Using Nested Loops

    Quote Originally Posted by Hello_World! View Post
    So tell me, when you first start learning something at school or university, do they expect you to learn everything on your own, or do they help you solve things ?

    I understand your point about spoon feeding, but the guy is asking for help, don't u think he can sit and think if he had to ? and if he knew that he needs help, then he would ask for it ? I don't understand why the strict rules about not posting solutions...you always learn ...even if u took someone else's code and studied it, you would still learn, I'm sure that you can write more than one piece of code to do the exact same thing. so why not post a solution, and let the person asking for help, get the idea, and then try to recode it. Or perhaps if you really want, maybe u can use by pesudo code, instead of complete code, which in a way makes them learn the syntax better.
    Did you read the link provided above? It contains much of the reasoning behind spoon feeding, which is supported by the moderating team here as well as every other forum you might visit. So I will leave it up to you to read and digest its points. Should you like to continue to discuss, then please make another thread, use the private messaging feature, or start a conversation rather than continuing in this thread

Similar Threads

  1. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 10:43 PM
  2. Please help! Nested while loops and asterisk triangles!
    By rockout341 in forum Loops & Control Statements
    Replies: 12
    Last Post: September 15th, 2011, 11:50 AM
  3. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 03:31 PM
  4. Nested If Else Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 6th, 2010, 02:46 PM
  5. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 12:14 AM