Results 1 to 3 of 3
  1. #1
    Stratgtar565 is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default 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.
    Java 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;
        }
        
      }
    }
    Last edited by Stratgtar565; 10-12-2011 at 05:27 AM.

  2. #2
    Stratgtar565 is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default 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.

  3. #3
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

Similar Threads

  1. How to replace int with asterisk
    By erin.ctm in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 11:13 PM
  2. Using an asterisk to split a string.
    By Cruncher in forum New To Java
    Replies: 2
    Last Post: 06-13-2010, 03:12 PM
  3. Create an Asterisk Pattern in Java
    By kaurpower in forum New To Java
    Replies: 1
    Last Post: 03-17-2010, 01:00 AM
  4. asterisk java
    By abhi in forum New To Java
    Replies: 4
    Last Post: 01-22-2008, 07:09 AM
  5. Asterisk-Java 0.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-02-2007, 08:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •