打印一个x,y矩阵,java [英] Print a x,y matrix, java
本文介绍了打印一个x,y矩阵,java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用循环打印followin矩阵:
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 +);
$ p $但是它打印: 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 +);
}
I want to print the followin matrix using for loops:
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
I use:
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 + "");
}
}
}
But it prints:
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
解决方案 Change your inner loop to this:
for (int p = w; p <= 6 + w; p++) {
System.out.print((p - 1) % 6 + 1 + " ");
}
这篇关于打印一个x,y矩阵,java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文