This question already has answers here:
How to print Two-Dimensional Array like table
(15个答案)
7年前关闭。
我想像表格一样打印输入的二维数组
即如果由于某种原因他们输入了全1 ...
就像上面一样,但是在Java Eclipse的控制台中,没有花哨的按钮和GUI,但是在控制台中,这就是我所拥有的...。
这是我输入所有内容并且控制台终止时得到的结果:
(15个答案)
7年前关闭。
我想像表格一样打印输入的二维数组
即如果由于某种原因他们输入了全1 ...
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
就像上面一样,但是在Java Eclipse的控制台中,没有花哨的按钮和GUI,但是在控制台中,这就是我所拥有的...。
import java.util.Scanner;
public class Client {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[][] table = new int[4][4];
for (int i=0; i < table.length; i++) {
for (int j=0; j < table.length; j++) {
System.out.println("Enter a number.");
int x = input.nextInt();
table[i][j] = x;
System.out.print(table[i][j] + " ");
}
System.out.println();
}
System.out.println(table);
}
}
这是我输入所有内容并且控制台终止时得到的结果:
Enter a number.
1
1 Enter a number.
1
1 Enter a number.
1
1 Enter a number.
1
1
[[I@3fa1732d
最佳答案
您需要与输入数字分开打印出数组。因此,您可以执行以下操作:
public class PrintArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[][] table = new int[4][4];
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table.length; j++) {
// System.out.println("Enter a number.");
int x = input.nextInt();
table[i][j] = x;
}
//System.out.println();
}
// System.out.println(table);
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
System.out.print(table[i][j] + " ");
}
System.out.println();
}
}
}