Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ASCII Triangle

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile 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:
    private int getTriangleWidth( int triangleHeight )
        {
            return 2 * triangleHeight - 1;
        }
    and the method I am trying to use the make the triangle is:
    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( 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.
    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 helloworld922; March 11th, 2010 at 07:41 PM. Reason: please use [code] tags!


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Location
    Home-Ujjain /Job-Mumbai
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: ASCII Triangle

    Hi dear I have modified your code try this. It will work now

    public class Main {
        public static void main(String[] args) {
            Main obj=new Main();
            obj.printTriangle(5);
        }
        private void printTriangle(int height)
        {
        	int n;
            for( n = 0; n < height; n++ )
            {
                for(int j=height;j>n;j--)
                      System.out.print(" ");
                for(int i=0;i<=n*2;i++)
                    System.out.print("*");
                System.out.print("\n");
            }
        }
    }
    Programmer

Similar Threads

  1. Triangle Printing - Java Program
    By mparthiban in forum Java Programming Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 10:00 AM
  2. Triangle issues
    By FrEaK in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 24th, 2010, 08:49 AM
  3. Equilateral Triangle Using nested for loop
    By uchizenmaru in forum Loops & Control Statements
    Replies: 2
    Last Post: January 14th, 2010, 10:01 PM
  4. Triangle Question
    By Leeds_Champion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 29th, 2009, 11:33 PM