Results 1 to 3 of 3

Thread: ASCII Triangle

  1. #1
    physics is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Question ASCII Triangle

    Hi,
    I need to create a triangle based on a height value given by the user.
    The base of the triangle is calculated with:

    Java Code:
    private int getTriangleWidth( int triangleHeight )
        {
            return 2 * triangleHeight - 1;
        }
    and the method I am trying to use the make the triangle is:

    Java Code:
    private void printTriangle( int height, int margin, boolean pointUp )
        {
        	int base = this.getTriangleWidth( height );
        	int m;
        	int n;
        	int o;
        	
        	if( pointUp )
        	{
    
                    for( n = 0; n < height; n++ )
    	    	{
    	    		for( m = 1; m <= margin; m++ )
    	        	{
    	        		System.out.print( " " );
    	        	} 
    
    	    	for( n = 0; n < height; n++ )
    	    	{	    		
    	    		for( o = -1; o < n; o++ )
    	        	{
    	        		System.out.print( "*" );
    	        	}
    	        	System.out.println( " " );
            
        		}
        	}
        }
    If the user inputs 5, this results with a triangle that looks like this (I haven't included my driver method that actually tests the output):
    *
    **
    ***
    ****
    *****

    but I need an equilateral triangle: if the user inputs 5, it should have a base of 9 and a height of 5 and it should not have a right angle as the one above.
    I understand that in my code above I don't have it depending on the base value at all but I'm not sure how to include it in or how to get the whitespace to come before the * in an even way so that I get my desired result.

    Any hints would be wonderful
    Last edited by physics; 03-12-2010 at 10:32 PM.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Here's a clue, you can use the getTriangleWidth(int) method to return the number of stars needed for each level of the triangle.

    You aren't even using that method yet...

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Also, the number of spaces on the left is half the difference between the widths of the longest row and the current row:

    (widthOfLongestRow - widthOfCurrentRow) / 2

    -Gary-

Similar Threads

  1. triangle program
    By computerbum in forum New To Java
    Replies: 8
    Last Post: 03-02-2010, 04:55 AM
  2. Triangle
    By jkswebsite in forum New To Java
    Replies: 8
    Last Post: 01-10-2009, 02:08 PM
  3. Is it a right triangle? (Code help)
    By TheApostle in forum New To Java
    Replies: 8
    Last Post: 10-07-2008, 08:39 PM
  4. Pascal Triangle help
    By Magic101 in forum New To Java
    Replies: 4
    Last Post: 05-01-2008, 07:51 PM
  5. Making triangle
    By banie in forum New To Java
    Replies: 4
    Last Post: 02-02-2008, 11:23 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
  •