Exercise 1C - Triangle Printing Program

Write a program that takes a number from the user and then prints a triangle of stars of that height. It then asks the user whether they want another triangle.

e.g.
Triangle Printing Program
=========================

Please enter desired height of triangle: 3

*
**
***



Another one? yes

Please enter desired height of triangle: .....

 

Hints

You should remember:

  1. that the class name needs to be the same as the filename.
    (so if you call your class Triangle, then it needs to be saved in Triangle.java)
  2. to use the public static void main( String argv[] ) method.
  3. to look at the number guessing program for hints on how to read numbers from the keyboard.
  4. that you should be using a couple of loops to print out the triangle.
  5. to lay out your code neatly - doing it in xemacs will automatically lay it out acceptably.
  6. to comment your code (preferably in the javadoc style used above).


Extras

Try changing the program so that the triangle of stars is centred on the screen:

e.g.
  *
 ***
*****