Oh Rocket Tree

From CompSciWiki
Revision as of 15:22, 4 December 2011 by JordanW (Talk | contribs)

Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Evergreens are a great pointy companion around the holidays. Or in the middle of July really. Today your going to draw one with text on the console using System.out. If you despise the idea of Christmas trees, imagine it's an awesome rocket ship!

 

By Students

People either love or hate 'shapes questions'. You?

Solution

Remember the outer for loop always determines the height of the shape. In this case the tree has an extra line, so the outer for loop will be something like for (int i = 0; i<=TREE_HEIGHT; i++). Also, remember that you only have to print the spaces to the left of the tree. HINT: this tree has a star on the top, giving it 6 rows, even though the TREE_HEIGHT is set at 5.

        *
     ***
    *****
   *******
  *********
 ***********
     ^^^ 

And that's enough of a rocket tree for my liking. For an extra little challenge, try putting a trunk / thrusters on that bad boy.

Code

Solution Code

Back to the Program-A-Day homepage