Java 生成金字塔以显示模块10中的数字
我试图生成的金字塔从数字1(1)开始,在每一行中,第一个数字决定了行数。此外,行中的第一个数字还确定了该顺序中的数字数量(根据模块10)。根据模块10,我们通过将左边的数字和上面的数字相加来计算一行中的所有其他数字Java 生成金字塔以显示模块10中的数字,java,Java,我试图生成的金字塔从数字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(modu
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
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
我遗漏了什么?您需要存储结果以使用它打印下一行
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();
}
}
}
公共类测试{
公共静态void main(字符串[]args){
int n=12;
int i,j;
int[]arr=新的int[n][n];
arr[0][0]=1;
System.out.println(arr[0][0]);
对于(i=1;i,您需要存储结果以使用它打印下一行
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();
}
}
}
公共类测试{
公共静态void main(字符串[]args){
int n=12;
int i,j;
int[]arr=新的int[n][n];
arr[0][0]=1;
System.out.println(arr[0][0]);
对于(i=1;i首先,您编写一个帮助器方法来计算要显示的数字。此方法将行索引和列索引或行号和列号作为参数,只要您比较熟悉。假设我们对行和列使用基于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;
}
这将为您提供预期的输出:
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
首先,您要编写一个帮助器方法来计算要显示的数字。此方法将行索引和列索引或行号和列号作为参数,以您更熟悉的方式作为参数。假设我们对行和列使用基于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;
}
这将为您提供预期的输出:
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
你的代码中没有任何地方可以“在上面添加数字”。你打算怎么做?如果你想添加上一个数字,我不认为你可以直接打印出来。@Progman我不知道怎么做,因为我需要获取当前数字的左上方数字。你的代码中没有任何地方可以“在上面添加数字”。你打算怎么做?如果你想添加上一个数字,我不认为你可以直接打印出来。@Progman我不知道怎么做,因为我需要得到当前数字的左上方数字。arr[I][j]=I+1;
应该是arr[I][j]=(I+1)%10;
arr[I][j]=I+1;
应该是arr[I][j] =(i+1)%10;
谢谢!注释有助于理解。您能告诉我您是如何实现此实现的吗?@DianaJ此帮助器方法和
循环的(与您已有的类似)已准备好使用。您无需再做任何事情。谢谢!注释有助于理解。您能告诉我您是如何实现此实现的吗?@DianaJ此帮助器方法和
循环的(与您已有的循环类似)已准备好使用。您无需再做任何事情。