简体   繁体   中英

Number Pyramid In Java

Im sorry for bothering for this small thing but really i feel confused about this. I want to get output like;

        9
       89
      789
     6789
    56789
   456789
  3456789
 23456789
123456789
 23456789
  3456789
   456789
    56789
     6789
      789
       89
        9

But im taking my output after 123456789 line. My codes are;

for(int column = 1; column <= 9; column++) {
         for(int row = 1; row <= 9; row++) {
          if(column <= row) { 
              System.out.print(row);
          } else {
            System.out.print(" ");
          }
         }
         System.out.println(' ');

    }

Thanks For Your Attention.

This is a bit dirty, but gives the desired output :

for(int column = -9; column <= 9; column++)
{
    if (column == 0) column = 2;
    for(int row = 1; row <= 9; row++)
    {
        if(Math.abs(column) <= row)
        { 
            System.out.print(row);
        } else 
        {
            System.out.print(" ");
        }
    }
    System.out.println();
}

This is a different approach that works

    String txt = "123456789";
    String msk = "         ";
    boolean done = false;
    boolean pos = true;
    int rows = 0;
    int c = 0;
    int maxrows=19;
    while (!done) {
        System.out.println(msk.substring(c) + txt.substring(9-c));
        if (pos) {
            c++;
            if (c>8) pos = false; 
        } else {
            c--;
            if (c<1) pos = true;
        }
        rows++;
        if (rows==maxrows) done = true;
    }

Here's a simple way:

    int num = 9;
    int spaces = 8;
    boolean decreasingSpaces = true;
    for (int row=0; row < 17; row++) {

        for (int i=0; i < spaces; i++)
            System.out.print(" ");

        for (int j=num; j <= 9; j++)            
            System.out.print(j);

        System.out.println();

        if (decreasingSpaces) {
            spaces--;
            num--;
            if (spaces < 0) {
                decreasingSpaces = false;
                spaces += 2;
                num += 2;
            }
        } else {
            spaces++;
            num++;
        }
    }
 /*output:
   54321
    5432
     543
      54
       5
 */
 class NumberPyramid1
 {
     public static void main(String args[])
     {
        int n=5;
        for(int i=1;i<=n;i++){
           for(int j=1;j<=i;j++){
              System.out.print(" ");
           }
           for(int k=n;k>=i;k--){
              System.out.print(k);
              //n--;
           }
           System.out.println();
           //n--;
         }
     }
 }
for(int column = 1; column <= 9; column++) {
     for(int row = 1; row <= 9; row++) {
      if(column <= row) { 
          System.out.print(row);
      } else {
        System.out.print(" ");
      }
     }
     System.out.println(); // println will create a newline

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM