Take an user-input, which is an integer indicating the number of levels in the tree.
and the output should look like this:
My java code after the jump :)
- the tree will print 1, 3, 5..... number of *
- you need to format it so the tree will alignment in the middle, i just use spaces here
- the stump needs to be right in the middle
import static java.lang.System.*;
import java.io.*;
import java.util.*;
public class Tree {
public static void main(String[] args)
{
Scanner scan = new Scanner(in);
out.print("please enter a number: ");
int temp = scan.nextInt();
int x = (temp-1)*2 +1; int y = x/2; // calculate how many spaces you need to print before the stump
int z = 1; // initial #of * you need to printfor(int i=0; i<temp; i++) //determines how many lines you print
{
for(int j=0; j<=y; j++) //determines how many spaces you print
{
out.print(" ");
}
for(int k = 0; k<z; k++) // determines how many * you print {
out.print("*");
}
out.println(); y--;
z+=2; //add 2 for the 1,3,5,7 increment
}
for(int i =0; i<=x/2; i++) //prints the spaces you need until you reach the middle
{
out.print(" ");
}
out.println("*"); // the stump!
}
}
hope this helps people who have similiar assignments :)
i'm not really good at explaining things, so i apologize if this is confusing
lame
ReplyDeletepost helped me a lot
ReplyDeleteonly printed half the tree
ReplyDeleteyou can do it with simpler code
ReplyDelete