aspose file tools*
The moose likes Beginning Java and the fly likes Pyramid Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pyramid" Watch "Pyramid" New topic
Author

Pyramid

Antonio Nobrega
Greenhorn

Joined: Apr 05, 2010
Posts: 11
Hey guys, i am kinda tired of this program i have to do, i cant manage to do it right.

Basically i have to print a X number of pyramids, each pyramid with a Y height.

So for example, if the user types, he wants 3 pyramids, first pyramid's height is 3, second pyramid's height is 2, third pyramid's height is 4 here's what i have to print:



I can't manage to come up with a code that prints that. Can someone help me?

Thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did you manage to print one pyramid ? Show us that code.


[My Blog]
All roads lead to JavaRanch
Antonio Nobrega
Greenhorn

Joined: Apr 05, 2010
Posts: 11
This is what i made:

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

So you've got one pyramid right There are probably different ways to print different pyramids one next to another. You can reuse some of the logic that you've already done.

Let's say you have three pyramids : 3, 2, 4
1. Find out which is the highest (highest=4)
2. Loop highest times ("row" = highest to 0 excluded)
2.1 Loop through each pyramids ("pyramidno" = 0 to 3 excluded)
2.1.1 Print the stars for the current "row" of the current pyramid (you need to know the max width of the current pyramid). If the current "row" is above the highest row of the current pyramid, print only spaces.

Try to build your program according to this algorithm, bits by bits. Start by making a method which returns the highest number of rows (step 1). etc...
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

Also, you can consider building the pyramids from the bottom up, this will be a lot easier. Just start with the bottom row, then work your way up.
Remember that you can add line separators to strings.
 
Don't get me started about those stupid light bulbs.
 
subject: Pyramid