i have to make a screen output final with variable which will set the height of atree.a the while loop has to be used.im kinda stuck to figuring this all out.

the tree has to look like this

*
***
*****
*******
*********
***

so far all i have is

public class MainTree {

public static void main(String[] args) {

int column = 1;
int first = column;
int height_of_tree = 6;
int last = height_of_tree;

while (column <= last)
{

int row = 2;
int space = last - 1;
int space_counter = -1;
space_counter++;

while (space != space_counter)
{
System.out.print(" ");
space = space - 1;
}

while (row <= column + 1)
{
System.out.print("*");
row++;
}

System.out.print("\n");
column++;
}


}


and i get
*
**
***
****
*****
******
facing only one right side

referin to ur sample tree the following would work i suppose.

int height = 9;
        int currentRow = 1; //current row count for iterating
        int stCount = 1; //star count for each row
        while(currentRow <= height){
            int checker = 1; //to keep check of already printed star

            if(currentRow == height){
                stCount = height/2;
                while(checker <= stCount){
                System.out.print("*");
                checker++;
                }
                System.out.println("");
                break;
            }
            while(checker <= stCount){
                System.out.print("*");
                checker++;
            }
            System.out.println("");
            stCount += 2; //in ur sample each consecutive row has 2 more stars than the previous row
            currentRow++;
        }

as for 2 sided, one soultion could be

int height = 12;
        int currRow = 1; //current row count (for iterating)
        int stCount = 1; //star count (for each row)

        int spaceCount = ((((height-1) * 4)-3))/2; //space count for each row except the last
        int spCountForLast = spaceCount-2; //space count for last row        
        while(currRow <= height){
            int stChecker = 1; //star checker (for each row)
            int spCheck = 1; //space checker (for each row)
            if(currRow == height){
                stCount = 5; //just assumed a constant width for the trunk
                while(spCheck <= spCountForLast){
                    System.out.print(" ");
                    spCheck++;
                }
                while(stChecker <= stCount){
                    System.out.print("*");
                    stChecker++;
                }
                System.out.println("");
                break;
            }
            while(stChecker <= stCount){
                while(spCheck <= spaceCount){
                    System.out.print(" ");
                    spCheck++;
                }
                System.out.print("*");
                stChecker++;
            }

            System.out.println("");
            currRow++;
            stCount = (currRow * 4)-3; 
            spaceCount -= 2;
        }

hope this helps.

Another solution that can adjust the size of the trunk.

import java.util.ArrayList;


public class Tree {

	public ArrayList<String> tree = new ArrayList<String>();
	private int height, trunkWidth, trunkHeight;
	
	public Tree() {
		configTree(5, 3, 1);
	}
	
	public Tree(int height) {
		configTree(height, 3, 1);
	}
	
	public Tree(int height, int trunkWidth, int trunkHeight) {
		configTree(height, trunkWidth, trunkHeight);
	}
	
	private void configTree(int height, int trunkWidth, int trunkHeight) {
		this.height = height;
		this.trunkWidth = trunkWidth;
		this.trunkHeight = trunkHeight;
		createTree();
	}
	
	public void createTree() {		
		int factor = height * 2 - 1;
		
		// Create Branches
		int base = factor;
		while (base > 0) {
			String branch = "";
			for (int i = 0; i <= factor; i++) {
				if (i < (factor - base) / 2)
					branch += " ";
				else if (i > factor - base)
					branch += "*";
			}
			tree.add(0, branch);
			base -= 2;
		}
		
		// Create Tree Trunk
		String trunk = "";
		for (int i = 0; i < factor; i++) {
			//if (i > (factor / 2 + 1 - TRUNK_SIZE) && i < (factor / 2 + TRUNK_SIZE - 1))
			if (i < (factor - trunkWidth) / 2)
				trunk += " ";
			else if (i < (factor - trunkWidth) / 2 + trunkWidth)
				trunk += "*";
		}
		for (int i = 0; i < trunkHeight; i++)
			tree.add(trunk);
	}
	
	private void print() {
		for (String branch : tree) 
			System.out.println(branch);
	}
	
	public static void main(String[] args) {
		Tree t = new Tree(10, 7, 3);
		t.print();
	}
}

Output

*
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************
      *******
      *******
      *******

Edited 5 Years Ago by ztini: n/a

This article has been dead for over six months. Start a new discussion instead.