Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default [SOLVED] little help!

    little help?
    this program displays
    1
    2 1
    3 2 1
    4 3 2 1
    5 4 3 2 1
    6 5 4 3 2 1

    and i want the same numbers to align like this
    1
    2 1
    3 2 1
    4 3 2 1
    5 4 3 2 1
    6 5 4 3 2 1
    Java Code:
    public class cc{
    	public static void main (String[]args){
    
    	int n = 6;
    
    	for (int i = 1; i<=n; i++){
    
    		[COLOR="SandyBrown"]for (int spa = 1; spa <= n-i; spa++)[/COLOR]
    
    		System.out.print(" ");
    
    		for(int j =i; j>=1; j--){
    
    			System.out.print((j <= 6) ? " "+j : " " + j);
    }
    
    System.out.println();
    }
    	}
    }
    i think i need to do something to this line>>>>>for (int spa = 1; spa <= n-i; spa++)

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    dang it man, you got to use more meaningful titles.
    this is like your 5th "help me" title. it helps peps search for stuff on google if you make your title more meaningful.

    just play around w/ the math.
    for (int spa = 1; spa <= ((n*2)-i-i); spa++)

    ---
    if the post is solved, mark it as solved by using the Thread Tools toolbar at the very top of this page.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    oh that don't work when variable n>9.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    if user can set n > 9, we should use method Math.log10

    Java Code:
    for (int spa = 1; spa <= ((n * (Math.floor(Math.log10(n)) + 2)) - ((Math.floor(Math.log10(n)) + 1) * i) - i); spa++)
    			{
    				System.out.print("x");
    			}
    			
    			for (int j = i; j >= 1; j--)
    			{
    				for(int k = 0; k < (Math.floor(Math.log10(n)) + 2) - (Math.floor(Math.log10(j)) + 1); k++)
    				{
    					System.out.print(".");
    				}
    				System.out.print(j);
    			}

  5. #5
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i hate logs! YOU WON THIS ROUND MTYOUNG... actually, i think the op is only allow to change one line.

    here's the complete working code from mtyoung's snippet.
    Java Code:
    * REMOVED [I](to be added after thread is solved.)[/I]
    
    public class cc{
      public static void main (String[]args){
        int n = 15;
    
        for (int i = 1; i<=n; i++){
          for (int spa = 1; spa <= ((n * (Math.floor(Math.log10(n)) + 2)) - ((Math.floor(Math.log10(n)) + 1) * i) - i); spa++)
          {
            System.out.print(" ");
          }
          
          for (int j = i; j >= 1; j--)
          {
            for(int k = 0; k < (Math.floor(Math.log10(n)) + 2) - (Math.floor(Math.log10(j)) + 1); k++)
            {
              System.out.print(" ");
            }
            System.out.print(j);
          }
          System.out.println();
        }
      }
    }
    Java Code:
    S:\TMP>java cc
                                                1
                                             2  1
                                          3  2  1
                                       4  3  2  1
                                    5  4  3  2  1
                                 6  5  4  3  2  1
                              7  6  5  4  3  2  1
                           8  7  6  5  4  3  2  1
                        9  8  7  6  5  4  3  2  1
                    10  9  8  7  6  5  4  3  2  1
                 11 10  9  8  7  6  5  4  3  2  1
              12 11 10  9  8  7  6  5  4  3  2  1
           13 12 11 10  9  8  7  6  5  4  3  2  1
        14 13 12 11 10  9  8  7  6  5  4  3  2  1
     15 14 13 12 11 10  9  8  7  6  5  4  3  2  1
    Last edited by angryboy; 01-30-2009 at 08:47 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  6. #6
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    oh if does work for 9 thats fine because this is a fixed number.
    sorry about the "help me" title ill try to be more creative next time. and thank you for your help!

    Mr. angeryboy can you explain this ((n*2)-i-i); to me? a little confusing.
    Last edited by PureAwesomeness; 01-30-2009 at 08:31 AM.

  7. #7
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    lol hey i'm a Mr.

    n = 6; meaning 6 rows, 6 cols of space. so n* 2 is double that.

    but to be honest, i just played around w/ the numbers a bit.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Posting Permissions

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