我想问你一些关于我的代码的问题。我想要显示如下输出:
2 3 4 1
6 7 8 5
10 11 12 9
14 15 16 13
但是输出如下所示:
2 3 4 1
6 3 0 -3
2 -1 -4 -7
-2 -5 -8-11
下面是我当前的代码:
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
System.out.printf("%3d", number);
if(j==2){
plus=-3;
}
number+=plus;
}
number+=8;
System.out.println("");
}
你能告诉我它出了什么问题吗?谢谢
发布于 2020-06-25 11:54:33
您需要在外部循环的每次迭代结束时将plus
重置为1
。请参阅操作here中的以下代码。
int number = 2;
int plus = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.printf("%3d", number);
if (j == 2) {
plus = -3;
}
number += plus;
}
number += 8;
plus = 1;
System.out.println("");
}
https://stackoverflow.com/questions/62567559
复制相似问题