Results 1 to 5 of 5

Thread: triangle

  1. #1
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default triangle

    Hi guys, i'm trying to print a triangle (which is taken from a command line argument) so that its longest row is at the top and the right hand side straight.

    I'm guessing another loop is required:

    Java Code:
    public class PrintTriangleMirror
    {
      public static void main(String [] args)
      {
        int height = Integer.parseInt(args[0]);
        
        for (int row = 1; row <= height; row++)
        {
          for (int column = 1; column <= row; column++)
            System.out.print("[_]");
    	
        
         System.out.println();
        }
      }
    }
    Any suggestions?

    Kind regards

    Shyam
    Last edited by Shyamz1; 11-07-2010 at 05:46 PM.

  2. #2
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    So do you have any suggestions on how to flip the triangle. I know another for loop is required... and you can think about the spaces.

    So you can use a for loop... but i dont what to include in it.

    kind regards

    Shyam

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  4. #4
    Shyamz1 is offline Member
    Join Date
    Oct 2010
    Posts
    56
    Rep Power
    0

    Default

    Yeah i know sorry, and i have edited the code and use code tags if you may look :). i used code and /code
    Last edited by Shyamz1; 11-07-2010 at 05:53 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    How about having two inner for loops in a row, one to print spaces, the other to print your lines:

    Java Code:
    outer loop -- loop through the rows
       inner loop 1
          print spaces
       end inner loop 1
    
       inner loop 2
          print "_"
       end inner loop 2
    
       println
    end outer loop

Similar Threads

  1. Triangle using Numbers
    By Anandt88 in forum New To Java
    Replies: 16
    Last Post: 06-05-2010, 04:10 PM
  2. ASCII Triangle
    By physics in forum New To Java
    Replies: 2
    Last Post: 03-13-2010, 01:00 AM
  3. triangle program
    By computerbum in forum New To Java
    Replies: 8
    Last Post: 03-02-2010, 04:55 AM
  4. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 02:08 PM
  5. Is it a right triangle? (Code help)
    By TheApostle in forum New To Java
    Replies: 8
    Last Post: 10-07-2008, 08:39 PM

Posting Permissions

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