如何格式化二维数组,以便打印出一种矩阵“样式”。

例如,在此代码数组片段中,得到两个二维int数组的乘积。

说一个像这样的2x2矩阵

   59 96

   78 51


在命令提示符中打印出来后,它最终像这样显示

59 96 78 51

如何使其以行和列的矩阵类型格式显示。

2X2只是该程序中的一个示例,二维数组必须大于或等于50。

            else
           {
            int[][] array;
           //this will hold the multiplied thing

            array=multiply(matrix,matrix2);

            System.out.println("this is the result of the multiplication");
            //print out the array
            for(int i=0; i<row; i++)
            {
                for( int j=0; j< col2; j++)
                {
                    System.out.print(array[i][j] + " \t");

                }
            }

最佳答案

这应该可以,但是以漂亮的方式进行这种格式化并不是那么容易。

for(int i=0; i<row; i++) {

        for( int j=0; j< col2; j++) {

            System.out.print(array[i][j] + " \t");
        }
        System.out.println();
}

关于java - 如何格式化数组,以便在打印时看起来像矩阵?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27993599/

10-10 21:10
查看更多