Results 1 to 2 of 2

Thread: For Loop

  1. #1
    kian_hong2000 is offline Member
    Join Date
    Aug 2008
    Posts
    15
    Rep Power
    0

    Default For Loop

    Hi...

    Anyone know how to display a triangle in the
    1) Center and pointing up position
    2) Center and pointing down position


    for(int i = 0; i < 5; i++)
    {
    for (int j = 0; j < 5; j++)
    {
    if(j >=i)
    {
    System.out.print("*");
    }
    else
    {
    System.out.print(" ");
    }
    }
    System.out.println();
    }

    I have tried for hours but have no idea how to do.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,886
    Rep Power
    25

    Default

    Take a piece of grid paper and draw in the figure you want.
    Then for each row of the figure, do a println of a string with the *s spaced as per the drawing. For example for the point up, the first line would be a single centered *. As you move down there would be 2 *s with increasing spaces between then and decreasing spaces before and after them. The bottom line would be a row of *s all the way across.

Similar Threads

  1. while loop
    By Unknown1369 in forum New To Java
    Replies: 5
    Last Post: 07-08-2008, 11:15 AM
  2. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:45 PM
  3. How to use While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:44 PM
  4. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 07:44 AM
  5. while loop
    By michcio in forum New To Java
    Replies: 5
    Last Post: 01-27-2008, 01:56 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
  •