航空座椅用于
airline seat using for
我希望我的结果像这样显示,每三个在一行
********Flight SA220*******
000
000
000
000
000
结果却变成了这样
**** Flight SA220 ****
000000000000000
有什么建议吗??
这是我的 java 代码
System.out.println("**** Flight "+flightCode[0]+" ****");
for(int j=0 ; j<seat[0].length ; j++)
for (int k=0; k< seat[0][j].length;k++)
if (seat[0][j].length%3 == 0)
System.out.println(seat[0][j][k]);
else
System.out.print(seat[0][j][k]);
在您的 if
中,您需要将 seat[0][j].length
更改为 k
。数组的长度永远不会改变,您需要检查索引(循环在数组中的位置)是否为 %3
System.out.println("**** Flight "+flightCode[0]+" ****");
for(int j=0 ; j<seat[0].length ; j++){
for(int k=0; k< seat[0][j].length;k++){
if(k%3 == 0){
System.out.println(seat[0][j][k]);
}
else{
System.out.print(seat[0][j][k]);
}
}
}
只需在内循环之后的外循环中添加System.out.print("\n");
或System.out.println();
即可。
您可以去掉 if 语句:
System.out.println("**** Flight "+flightCode[0]+" ****");
for(int j=0 ; j<seat[0][0].length ; j++)
{
for (int k=0; k< seat[0].length;k++)
System.out.print(seat[0][k][j]);
System.out.println();
}
我希望我的结果像这样显示,每三个在一行
********Flight SA220*******
000
000
000
000
000
结果却变成了这样
**** Flight SA220 ****
000000000000000
有什么建议吗??
这是我的 java 代码
System.out.println("**** Flight "+flightCode[0]+" ****");
for(int j=0 ; j<seat[0].length ; j++)
for (int k=0; k< seat[0][j].length;k++)
if (seat[0][j].length%3 == 0)
System.out.println(seat[0][j][k]);
else
System.out.print(seat[0][j][k]);
在您的 if
中,您需要将 seat[0][j].length
更改为 k
。数组的长度永远不会改变,您需要检查索引(循环在数组中的位置)是否为 %3
System.out.println("**** Flight "+flightCode[0]+" ****");
for(int j=0 ; j<seat[0].length ; j++){
for(int k=0; k< seat[0][j].length;k++){
if(k%3 == 0){
System.out.println(seat[0][j][k]);
}
else{
System.out.print(seat[0][j][k]);
}
}
}
只需在内循环之后的外循环中添加System.out.print("\n");
或System.out.println();
即可。
您可以去掉 if 语句:
System.out.println("**** Flight "+flightCode[0]+" ****");
for(int j=0 ; j<seat[0][0].length ; j++)
{
for (int k=0; k< seat[0].length;k++)
System.out.print(seat[0][k][j]);
System.out.println();
}