我使用了多维来创建一个表格,但是当我运行它时,表格似乎水平移动,如何在运行时使其垂直排列?
public static void main(String args[]){
int firstarray[][]={{1,2,3,4,5}
,{6,7,8,9,10}};
int secondarray[][]={{30,31,32,33,}
,{43},{4,5,6}};
System.out.println("This is the first array");
display(firstarray);
System.out.println("This is the second array");
display(secondarray);
}
public static void display (int x[][]){
for(int row=0;row<x.length;row++){
for(int column=0;column<x[row].length;column++){
System.out.print(x[row][column]+"\t");
}
}
}
}
答案 0 :(得分:0)
在内循环后断开每一行:
for (int row=0; row<x.length; row++) {
for (int column=0; column<x[row].length; column++) {
System.out.print(x[row][column]+"\t");
}
System.out.println(); // Break current line
}
答案 1 :(得分:0)
Inner Loop打印出为您生成每一行的每一列,因此在每行的末尾您必须使用System.out.println
转到与外循环相关的下一行。在更好的场景中,如果你想要新行,在阅读下一行的新列之前,你必须使用System.out.println`
希望此示例可以帮助您更好地学习
代码:
int a[][] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
for (int i = 0; i < a.length; i++) {
System.out.print("---> It is going to read colmuns in order to make a line");
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j]+" ");
}
System.out.println("-->It is going to make a new next line");
}
输出:
---> It is going to read colmuns in order to make a line 1 2 3 4 5 -->It is going to make a new next line
---> It is going to read colmuns in order to make a line 6 7 8 9 10 -->It is going to make a new next line