Results 1 to 2 of 2
  1. #1
    Dai
    Dai is offline Member
    Join Date
    Sep 2013
    Posts
    15
    Rep Power
    0

    Default Help Numeric Triangle java

    So I have to make a triangle figure just like you see below and I can't get it right, first of all the user has to enter "3" in order to print the triangle and I'm having problems with math, so I get 1 and 2 and 3 but I cant find where exactly decree (--) to go from 2 to 0 and then back to 2 and so on.

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
             /*       1
              * 
              *    2  0  2
              *    
              *  3 0  0  0  3
              * 
              */
            
    public class Triangle {
    
    	public static void main(String[] args) throws NumberFormatException, IOException {
    		System.out.println ("Enter number 3");
    		BufferedReader reader=new BufferedReader (new InputStreamReader(System.in));
    		int number=Integer.parseInt(reader.readLine ());
    		
    		
    		int numOfColumns=1;
    		int start=0;
    		int numOfSpaces=number/2;
    		for( int i=1; i<=number; i++)
    		{
    			if (i<number/2+1)
    			{
    				start=i;
    			}
    			else
    			{
    				start=(number+1)-i;
                    numOfSpaces=numOfSpaces+1;
    			}
    			for (int j=1; j<=numOfSpaces; j++)
    			{
    				System.out.print(" ");
    			}
    			numOfSpaces=numOfSpaces-1;
    			for (int j=1; j<=numOfColumns; j++)
    			{
                              int middleColumn=numOfColumns/2+1;
    			System.out.print(start);
                            if (j<middleColumn)
                            {
                             start--;
                           
    			}
                            else
                             {
                             start++;
                             }
    			}
    			System.out.println ();
    			if (i<number/2)
    			{
    				numOfColumns = numOfColumns+2;
    			}
    			else
    			{
    				numOfColumns = numOfColumns-2;
    				break;
    			}
    		}
    	}
    
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Help Numeric Triangle java

    Think about the algorithm. On each line you have s number of spaces followed by a number n. For each subsequent line the number of spaces decreases and the number increases. Take it a step at a time, try getting the following to output before going any further:
    Java Code:
            1
        2
    3

Similar Threads

  1. Replies: 1
    Last Post: 08-12-2013, 10:59 AM
  2. Replies: 4
    Last Post: 11-18-2012, 11:43 PM
  3. Replies: 7
    Last Post: 11-18-2012, 05:22 AM
  4. Help! How to make triangle with java
    By java greek in forum JCreator
    Replies: 1
    Last Post: 09-07-2009, 03:15 PM
  5. Help! How to make triangle with java
    By java greek in forum New To Java
    Replies: 4
    Last Post: 09-07-2009, 03:09 PM

Tags for this Thread

Posting Permissions

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