public class F2E12 {
public static final int DIM = 5;
public static void main(String[] args) {
printMatrix(DIM);
}
public static void printMatrix(int n) {
int i = 0;
int j = 0;
for(;i<=n;i++) {
for(;j<=n;j++) {
System.out.print(j + " ");
}
System.out.print("\n");
}
}
}
我想打印一个矩阵,它将每行的第一个数字递增一个。 上面的代码应该产生:
- 0 1 2 3 4 5
- 1 0 1 2 3 4
- 2 1 0 1 2 3
- 3 2 1 0 3 4
- 4 3 2 1 0 1
- 5 4 3 2 1 0
而是打印出来。 “0 1 2 3 4 5”
最佳答案
public static void printMatrix(int n) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
System.out.print(Math.abs(j - i) + " ");
}
System.out.print("\n");
}
}
关于for循环中的Java增量声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52353989/