-
Java Asterisk Tree
Hello, all; I'm currently trying to find a way to make an asterisk tree in Java using only nested while loops, if statements, and the like. I've already found a way to do it with for loops, but I'm required to only use while loops within the program. It needs to take user input of how large the tree should be (shich I use scanner for), and then have an output like this:
*
***
*****
*******
*********
***
***
***
I've persisted at the problem for a few days, and despite my being able to create a wedge of stars using while loops, I can't figure this one out. I don't want an explicit answer; just hints is fine, so that I may learn from this problem. Thank you all!
Here is my code.
Code:
import java.util.Scanner;
class JavaTree{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int ast, i=1, rows, spaces;
System.out.println("How large should the tree be?");
rows = scan.nextInt();
spaces = rows;
while (rows > 0){
while (spaces > 0){
System.out.print(" ");
spaces--;
}
ast = 2*i-1;
while (ast > 0){
System.out.print("*");
ast--;
}
i++;
System.out.println("");
rows--;
spaces = rows;
}
}
}
-
Re: Java Asterisk Tree
Sorry, my post only shows half of the tree. There should be another side to it on the left that makes it symmetrical.
-
Re: Java Asterisk Tree