How to print Two-Dimensional Array like table

后端 未结 15 1112
一整个雨季
一整个雨季 2020-11-30 06:55

I\'m having a problem with two dimensional array. I\'m having a display like this:

1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 . . . etc

What basi

15条回答
  •  长情又很酷
    2020-11-30 07:28

    public static void main(String[] args) {
        int[][] matrix = { 
                           { 1, 2, 5 }, 
                           { 3, 4, 6 },
                           { 7, 8, 9 } 
                         };
    
        System.out.println(" ** Matrix ** ");
    
        for (int rows = 0; rows < 3; rows++) {
            System.out.println("\n");
            for (int columns = 0; columns < matrix[rows].length; columns++) {
                System.out.print(matrix[rows][columns] + "\t");
            }
        }
    }
    

    This works,add a new line in for loop of the row. When the first row will be done printing the code will jump in new line.

提交回复
热议问题