Results 1 to 10 of 10
Thread: Upside down Triangle pattern
- 06-06-2011, 04:37 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
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.
But it rolls infinitely... :SJava 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(); }
Any help is greatly appreciated!
- 06-06-2011, 04:53 AM #2
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.
- 06-06-2011, 04:53 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
Deconstruct your inner for loop.
- 06-06-2011, 05:00 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
What do you mean deconstruct my inner loop?
- 06-06-2011, 05:02 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 23
- Rep Power
- 0
- 06-06-2011, 05:10 AM #6
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
Why is one of the i's ever be -1? if i = n, and j = i? I'm a little confused.
- 06-06-2011, 05:18 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
I also get an infinite loop when I change the parameter to j>=i.
- 06-06-2011, 05:24 AM #8
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
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?
- 06-06-2011, 05:25 AM #9
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.
- 06-06-2011, 05:46 AM #10
Member
- Join Date
- Jun 2011
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Triangle Pattern with numbers
By JayP in forum New To JavaReplies: 2Last Post: 06-05-2011, 07:27 PM -
Star triangle pattern
By crazy4fun in forum New To JavaReplies: 7Last Post: 03-06-2011, 01:03 PM -
BufferedImage flips the chart upside down
By sg3232 in forum Java 2DReplies: 0Last Post: 02-10-2011, 08:46 PM -
strategy pattern and bridge pattern
By jomypgeorge in forum New To JavaReplies: 2Last Post: 12-13-2010, 05:13 AM -
Class pattern to generate following pattern:-
By vxs in forum New To JavaReplies: 5Last Post: 07-14-2010, 11:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks