Results 1 to 10 of 10
  1. #1
    JayP is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default Upside down Triangle pattern

    Hi Sorry if this is something that is often posted but I don't understand why the way I did it doesn't work and I'd like to find out why it doesn't.

    The first triangle i did started as such
    *
    **
    ***...

    This time I'd like to switch it upside down as follows
    ***
    **
    *

    I did this by using the loop the opposite as I did with the first and instead of having j = 0 and incrementing it, i made j = n and decremented j.

    Java Code:
     	 // loop N times, one for each row
            for (int i = n; i <= n; i--) {
    
                // print j periods
                for (int j = i; j <= i; j--)
                    System.out.print("*");
    
                // print a new line
                System.out.println();
    		}
    But it rolls infinitely... :S

    Any help is greatly appreciated!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Assume i = 1
    You assign the value of i to j, j is 1.
    Is j <= i? Is 1 less than or equal to 1? Yes.
    Decrement j, j is now 0;
    Is j <= i? Is 0 less than or equal to 1? Yes.
    Decrement j, j is now -1;
    Is j <= i? Is -1 less than or equal to 1? Yes.
    etc

    Questions about these star shape programs are always popping up on forums. My advice always is to write another method which takes 2 parameters: the character to be printed on a single line and the number of times it should be printed. Then back in your main method you have a loop that iterates "lines" number of times calling the print method. Now all you have to do is work out the algorithm for how many *'s (and possibly spaces) need to be printed on wach line.
    Last edited by Junky; 06-06-2011 at 05:07 AM.

  3. #3
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Deconstruct your inner for loop.

  4. #4
    JayP is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    What do you mean deconstruct my inner loop?

  5. #5
    airowe is offline Member
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by JayP View Post
    What do you mean deconstruct my inner loop?
    See Junky's post.

    You've setup an infinite loop with the j <= -1 condition.

  6. #6
    JayP is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    Why is one of the i's ever be -1? if i = n, and j = i? I'm a little confused.

  7. #7
    JayP is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    I also get an infinite loop when I change the parameter to j>=i.

  8. #8
    JayP is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

    Default

    Oh, I see, since j will always be bellow i. So is my entire methodology wrong by using decrements to trying to invert the triangle?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    It is because your logic is wrong and you are just guessing. That does not work when it comes to programming. Walk away from the computer, grab a pencil and some paper, write down how many stars appear on each line. Do this for 3 rows, 4 rows, 5 rows etc. Then see if a pattern emerges. Once you have done that you can write the code.

    Also did you ignore my advice about writing a separate method? If you do write this method you should find writing the code much easier. Plus it can be used for all such programs.

  10. #10
    JayP is offline Member
    Join Date
    Jun 2011
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Triangle Pattern with numbers
    By JayP in forum New To Java
    Replies: 2
    Last Post: 06-05-2011, 07:27 PM
  2. Star triangle pattern
    By crazy4fun in forum New To Java
    Replies: 7
    Last Post: 03-06-2011, 02:03 PM
  3. BufferedImage flips the chart upside down
    By sg3232 in forum Java 2D
    Replies: 0
    Last Post: 02-10-2011, 09:46 PM
  4. strategy pattern and bridge pattern
    By jomypgeorge in forum New To Java
    Replies: 2
    Last Post: 12-13-2010, 06:13 AM
  5. Class pattern to generate following pattern:-
    By vxs in forum New To Java
    Replies: 5
    Last Post: 07-14-2010, 11:15 PM

Posting Permissions

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