像Java一样在Java中打印2d数组

Ada*_*nes 2 java eclipse arrays

我想打印一个像表格一样的输入二维数组,即如果由于某种原因他们放入了所有1 ...

1 1 1 1 

1 1 1 1 

1 1 1 1

1 1 1 1 
Run Code Online (Sandbox Code Playgroud)

就像上面这样,但在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我输入所有内容并且控制台终止时,这就是我得到的:

Enter a number.

1

1 Enter a number.

1

1 Enter a number.

1

1 Enter a number.

1

1 

[[I@3fa1732d
Run Code Online (Sandbox Code Playgroud)

Obi*_*ere 6

考虑使用java.util.Arrays.

那里有一种叫做的方法deepToString.这将在这里工作得很好.

System.out.println(Arrays.deepToString(table));
Run Code Online (Sandbox Code Playgroud)

相关:在Java中打印数组的最简单方法


ton*_*nga 1

您需要将数组与输入数字分开打印。所以你可以这样做:

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)