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!
|
|
Getting Started
|
Solution
The solution...
|
Code
Solution Code
class RocketTree
{
private static final int TREE_HEIGHT = 10;
public static void main(String args[])
{
for (int a = 0; a < TREE_HEIGHT; a++)
{
// Make some inital space
printChar(TREE_HEIGHT - a, ' ');
// Draw zee tree
printChar(a * 2, '*');
System.out.println();
}
printChar(TREE_HEIGHT - 2 + 1, ' ');
printChar(2, '^');
System.out.println();
}
static void printChar(int howMuch, char out)
{
for (;howMuch > 0; howMuch--)
{
System.out.print(out);
}
}
}
|
Back to the Program-A-Day homepage