简体   繁体   English

java 倒金字塔?

[英]java inverted pyramid of numbers?

I was asked to create a java program that will display an inverted pyramid of numbers (1 to 9 only).我被要求创建一个 java 程序,该程序将显示倒金字塔的数字(仅限 1 到 9)。 with user input of how many number of rows.用户输入多少行。

I don't know how to loop and limit the numbers to 9 and change it to the right side.我不知道如何循环并将数字限制为 9 并将其更改为右侧。

My code is:我的代码是:

    public static void main(String[] args) {
        Scanner sc = new Scanner (System.in);
        
        System.out.print("Enter height:\t");
        int height = sc.nextInt();
        
        for (int row = height; row >= 1; row--) {
            for (int col = 1; col <= row; col++) {
                System.out.print("");
            }
            for (int k = 1; k <= row; k++) {
                System.out.print(row + "");
            
            }
            System.out.println();
        }
        
        
    }
}

the output is: output 是:

Enter height:   12
121212121212121212121212
1111111111111111111111
10101010101010101010
999999999
88888888
7777777
666666
55555
4444
333
22
1

the output should be: output 应该是:

Enter height:  12
    111111111111
     22222222222
      3333333333
       444444444
        55555555
         6666666
          777777
           88888
            9999
             111
              22
               3

OR:或者:

Enter height:  20
11111111111111111111
 2222222222222222222
  333333333333333333
   44444444444444444
    5555555555555555
     666666666666666
      77777777777777
       8888888888888
        999999999999
         11111111111
          2222222222
           333333333
            44444444
             5555555
              666666
               77777
                8888
                 999
                  11
                   2

Your main loop is running from height to 0 which is causing the numbers you print to be from height to 1 where as output expects it in ascending order.您的主循环从高度运行到 0,这导致您打印的数字从高度到 1,而 output 期望它按升序排列。

output further expects that any value of height greater than 9 be treated as single digit increments again so after 9 comes 1 again instead of 10. output 进一步预计任何大于 9 的高度值都会再次被视为单位数增量,因此在 9 之后再次出现 1 而不是 10。

you have the write thought by appending space but to append space in java you actually have to add space in between the two ""你有写的想法,通过附加空间,但到 java 中的 append 空间,你实际上必须在两个“”之间添加空间

Example:例子:

System.out.print(" ").

Fixing all above issues code snippet should look something like this:修复上述所有问题的代码片段应如下所示:

    public static void main(String[] args) {
        Scanner sc = new Scanner (System.in);
        
        System.out.print("Enter height:\t");
        int height = sc.nextInt();
        
        for (int row = 1; row <=height; row++) {
            for (int space = 1; space < row; space ++) {
                System.out.print(" ");
            }
            for (int val = row ; val <= height; val++) {
                if(row>9){
                    System.out.print(row%10+1);
                }
                else{
                     System.out.print(row);
                }
            }
            System.out.println();           
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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