我想使用 for 循环打印以下矩阵:
1 2 3 4 5 6
2 3 4 5 6 1
3 4 5 6 1 2
4 5 6 1 2 3
5 6 1 2 3 4
6 1 2 3 4 5
我使用:
public static void main ( String [ ] args ) {
for(int w = 1; w <= 6; w++){
System.out.println("");
for(int p = w; p <= 6; p++){
System.out.print(p + "");
}
}
}
但它打印:
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
最佳答案
将内部循环更改为:
for (int p = w; p <= 6 + w; p++) {
System.out.print((p - 1) % 6 + 1 + " ");
}
关于java - 打印 x,y 矩阵,java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3980832/