有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java生成金字塔以显示模块10中的数字

我试图生成的金字塔从数字1(1)开始,在每一行中,第一个数字决定了行数。此外,行中的第一个数字还确定了该顺序中的数字数量(根据模块10)。根据模块10,我们通过将左边的数字和上面的数字相加来计算一行中的所有其他数字

int n = 12;
int i, j;

for (i = 1; i <= n; i++) {
    for (j = 0; j <= i; j++) {
      int module = i % 10;

      System.out.print(module + " ");

    }

    System.out.println();
  }
}

通过我的实现,我得到了

1 1 
2 2 2 
3 3 3 3 
4 4 4 4 4 
5 5 5 5 5 5 
6 6 6 6 6 6 6 
7 7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 9 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1 
2 2 2 2 2 2 2 2 2 2 2 2 2 

实际结果应该是

1
2 3
3 5 8
4 7 2 0
5 9 6 8 8
6 1 0 6 4 2
7 3 4 4 0 4 6
8 5 8 2 6 6 0 6
9 7 2 0 2 8 4 4 0
0 9 6 8 8 0 8 2 6 6
1 1 0 6 4 2 2 0 2 8 4
2 3 4 4 0 4 6 8 8 0 8 2

我错过了什么


共 (2) 个答案

  1. # 1 楼答案

    您需要存储结果,以便使用它打印下一行

    public class Test {
      public static void main(String[] args) {
        int n = 12;
        int i,j;
        int[][] arr = new int[n][n];
    
        arr[0][0] = 1;
        System.out.println(arr[0][0]);
    
        for(i = 1;i<n;i++) {
          for(j=0;j<=i;j++) {
            if(j == 0) {
              arr[i][j] = (i+1)%10;
            }else {
              arr[i][j] = (arr[i][j-1] + arr[i-1][j-1])%10;
            }
            System.out.print(arr[i][j]+" ");
          }
          System.out.println();
        }
      }
    
    }
    
  2. # 2 楼答案

    首先,编写一个助手方法来计算要显示的数字。此方法将行索引和列索引或行号和列号作为参数,以您更熟悉的方式为准。假设我们对行和列使用基于0的索引,我们可以为该方法定义以下值:

    • 当列索引为0(因此我们在第一列中)时,返回值是行索引+1模10
    • 当行索引为0(因此我们位于顶部)时,返回值为1
    • 对于所有其他位置,我们使用helper方法的递归调用将“左边的数字”与“上面的数字”相加

    助手方法如下所示:

    /**
     * Calculate the digits. The arguments are 0 based.
     * 
     * @param row The row index
     * @param column The column index
     * @return The digit to display
     */
    public static int calculateDigit(int row, int column) {
        if (row < 0) {
            throw new IllegalArgumentException("The row index must not be negative");
        }
        if (column < 0) {
            throw new IllegalArgumentException("The column index must not be negative");
        }
        if (column == 0) {
            return (row+1) % 10; // simply return the row number
        }
        if (row == 0) {
            return 1; // in the first row, so it is the "1"
        }
        // calculate the new number based on the expression
        // "adding the number from the left and the number above it"
        int left = calculateDigit(row, column-1);
        int above = calculateDigit(row-1, column-1);
        int sum = left + above;
        int modulo = sum % 10;
        return modulo;
    }
    

    您可以在已有的简单两个for循环中使用这个helper方法

    for (int r=0; r<10; r++) {
        for (int c=0; c<=r; c++) {
            int value = calculateDigit(r, c);
            System.out.print(value+" ");
        }
        System.out.println();
    }
    

    这将为您提供预期的输出:

    1 
    2 3 
    3 5 8 
    4 7 2 0 
    5 9 6 8 8 
    6 1 0 6 4 2 
    7 3 4 4 0 4 6 
    8 5 8 2 6 6 0 6 
    9 7 2 0 2 8 4 4 0 
    0 9 6 8 8 0 8 2 6 6