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

Thread: Help trying to get this patter

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

    Default Help trying to get this patter

    The program is suppose to print. I don't a way to space it

    Pattern
    1 2 3 4
       1 2 3
          1 2
             1

    for(int i = 4; i >= 1; i--){
    			for(int space = 1; space <= i; space++){
    				System.out.print(space);
    			}
    			System.out.println();
    		}


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help trying to get this patter

    Looks like you just need to add a space between each number and indent each line. Just think it through some more as to how to go about adding the indents and such.

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

    Default Re: Help trying to get this patter

    Would I have to use another nested loop? or an if statement? I've been trying for 2 days, but I don't seem to get a right solution

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

    Default Re: Help trying to get this patter

    Does anybody know how to explain where I would include the spaces?

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help trying to get this patter

    ...edited by moderator
    Last edited by copeg; June 15th, 2012 at 02:12 PM. Reason: spoonfeeding

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help trying to get this patter

    @Ikleen, please read the forum rules as well as the following: http://www.javaprogrammingforums.com...n-feeding.html
    Your post has been edited.

  7. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Help trying to get this patter

    Quote Originally Posted by wolf_fcknHaley33 View Post
    Does anybody know how to explain where I would include the spaces?
    I have a suggestion:

    Step away from the compiler for a minute and pretend that you are going to write a letter to a friend with some instructions so that he/she can reproduce that table. Your friend has a text editor on his/her computer, but has no way to compile and run a Java program.

    How would you describe the way to create the table using a text editor? (That's a rhetorical question, I don't necessarily need to see your natural language description.)

    Now, you find that your friend doesn't have a Java compiler, and doesn't know the language, so you can't just mail your Java source. You know that your friend does have some kind of compiler, but you don't know what it is and pretend that you don't know any other computer languages anyhow.

    So, you might send some pseudo-code that describes the process

    Maybe something like the following;

    First of all, forget the leading spaces for a minute. Suppose you want to
    reproduce
    1 2 3 4
    2 3 4
    3 4
    4

    How might it be described in pseudo-code?

    Maybe something like:

    // Declare two variables:
    //    numLines is the total number of lines to print.
    //      This is fixed at the beginning of the program.
    //
    //    numOnLine is the number of digits on each line
    //      First line : numOnLine is equal to numLines
    //      Second line: it is one less than on the previous line
    //      Third line : it is one less than on the previousline
    //      Etc.
    //
    // Here's the program in pseudo-code
    Set the value numLines equal to, say, 4
    Set the value of numOnLine equal to numLines
     
    for (int i = 0; i < numLines; i++)
    BEGIN LOOP // Outer Loop
       for (int j = 0; j < numOnLine; j++)
       BEGIN LOOP // Inner Loop
           Print decimal number i+1
           Print a space
       END LOOP  //  End Inner Loop
       Print a newline
       //
       // Put a statement here to adjust the value of numOnLine for the next time through the Outer Loop
       //
    END LOOP // End Outer Loop


    Now, we get to the Good Stuff: You want to print some leading spaces for each line, as follows (at least that's what I discovered when I created the table using my trusty text editor):
    First line : Number of leading spaces = 0
    Second line: Number of leading spaces = 3
    Third line : Number of leading spaces = 6
    Etc.

    How about something like this:

    // Same as above, but now there is another variable
    //    leadingSpaces is the number of space chars before the
    //    first digit on each line
    //      The first line:  leadingSpaces is equal to 0
    //      The second line: leadingSpaces is three more than for the previous line
    //      The third line:  leadingSpaces is three more than for the previous line
    //
     
     
    // So, here is the enhanced program in pseudo-code
    //
    Set the value numLines equal to, say, 4
    Set the value of numOnLine equal to numLines
    Set the value of leadingSpaces equal to zero
     
    for (int i = 0; i < numLines; i++)
    BEGIN LOOP // Outer loop
     
       for (int j = 0; j < leadingSpaces; j++)
       BEGIN LOOP // First Inner Loop
           Print a space character
       END LOOP   // End First Inner Loop
     
       for (int j = 0; j < numOnLine; j++)
       BEGIN LOOP // Second Inner Loop
           Print number for i+1 and print a space
       END LOOP  //  End Second Inner Loop
     
       // Put a statement here to adjust the value of numOnLine
       // Put a statement here to adjust the value of leadingSpaces
     
    END LOOP // End of Outer Loop

    Now, if that description doesn't make sense or it leads to something not consistent with your requirements, change it so that it does make sense and it does lead to a solution. Then (and only then) go back to your computer and whip out some Java code.

    Anyhow, see how it goes? Get away from Java for a minute and think (yes think) about what you need to do.

    Write it down! A program specification in a natural language might be appropriate. Pseudo-code can lead directly to implementation. Do whatever works best for you.

    Then, the actual implementation is a simple. (Or, at least it will be, provided your description is of a reasonable process to begin with.)

    The bottom line is this: How the heck can you write a program to carry out a sequence of operations if you haven't articulated the steps in a way that you have complete, detailed understanding of the process?

    Once you have written a few (or a few dozen or a few hundred) programs, you may rely on your experience and your memory of previous success stories and skip the pseudo-code part, but it's always possible to go back and use something like that if you ever get stuck again.

    I mean, it's always OK to ask, but...



    Cheers!

    Z
    Last edited by Zaphod_b; June 15th, 2012 at 10:30 PM.