SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Sep 2001
    Location
    St. Louis, MO
    Posts
    212
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using loops to print a christmas tree to screen

    I am going through an online Java course and have come across a programming exercise that has me stumped. It is in the second chapter of using loops fairly early on in the course.
    I was able to figure out the first exercise...
    Write a program that will write out a wedge of stars. The user will enter the initial number of stars, and the program will write out lines of stars where each line has one few star than the previous line:
    Initial number of stars:
    7
    *******
    ******
    *****
    ****
    ***
    **
    *
    Code:
    System.out.println("Enter a number:");
    String $amount = stdin.readLine();
    int $amt = Integer.parseInt($amount);						
    System.out.println();
    while($amt > 0)
    {
      int $stars = $amt;
      while($stars > 0)
      {
    	System.out.print("*");
    	$stars--;
      }
      System.out.println(); 
      $amt--;			 
      } 
    }
    This exercise is the one I can't figure out...
    Write a program that will write a tree made of stars on the terminal:
    *
    ***
    *****
    *******
    *********
    ***********
    *************
    ***************
    ***
    ***
    ***
    This is obviously not terribly important, but I would like to see the code. I want to understand everything before I go on to each chapter and I've spent an entire day on this with no luck.
    Thanks for any help!

  2. #2
    SitePoint Addict
    Join Date
    Sep 2001
    Location
    St. Louis, MO
    Posts
    212
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For some reason, when I pasted the 'tree' in, only half of it showed up. The lesson called for the entire tree to be displayed. Basically a triangle with three rows of 3 stars each as the trunk (just like above).


Bookmarks

Posting Permissions

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