简体   繁体   中英

Sideways Number Pyramid With Spaces Java

Sorry for asking such a simple question, but I've been stuck on this for a while. I need to have a program print out a sideways pyramid with increasing numbers and with spaces between each number. I have done star pyramids with for loops before, but I am unable to make a sideways one with numbers like this:
10
9 10
8 9 10
7 8 9 10
6 7 8 9 10
5 6 7 8 9 10
4 5 6 7 8 9 10
3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10
3 4 5 6 7 8 9 10
4 5 6 7 8 9 10
5 6 7 8 9 10
6 7 8 9 10
7 8 9 10
8 9 10
9 10
10

This is what I tried so far, it prints the right pyramid, but I can't get the spaces between the numbers to work.

      int n = 10;

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

Sorry for the bad formating, I am new here and also new to programing.

Hope this helps:

for (i = 0; i < 10; i++) {
  for (int j = 1; j <= 10; j++) {
    if (j < 10-i) {
      System.out.print("  ");
    } else {
      System.out.print(j + " ");
    }
  }
  System.out.println();
}

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