gpt4 book ai didi

java - 在 Java 中像表格一样打印二维数组

转载 作者:行者123 更新时间:2023-12-01 12:40:35 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to print Two-Dimensional Array like table

(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

最佳答案

考虑使用 java.util.Arrays .

里面有个方法叫deepToString .这将在这里工作得很好。

System.out.println(Arrays.deepToString(table));

此处相关: Simplest way to print an array in Java

关于java - 在 Java 中像表格一样打印二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20960055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com