Java 使用二维数组矩阵格式化时出现问题

Java 使用二维数组矩阵格式化时出现问题,java,arrays,matrix,methods,Java,Arrays,Matrix,Methods,我需要我的矩阵看起来完全像这样。(在文本下排列数字) 我目前得到的结果是这样的 Here are the two matrices, and the result when added: 8 5 6 6 3 8 2 3 11 13 8 9 7 7 4 5 4 9 2 1 11 16 6 6 9 4 4 8 5 1 1 1

我需要我的矩阵看起来完全像这样。(在文本下排列数字)

我目前得到的结果是这样的

Here are the two matrices, and the result when added:
   8   5   6   6        3   8   2   3       11  13   8   9
   7   7   4   5        4   9   2   1       11  16   6   6
   9   4   4   8        5   1   1   1       14   5   5   9
   4   2   7   7   +    7   9   1   3   =    11  11   8  10
   4   3   5   3        5   6   8   7        9   9  13  10
   4   2   2   1        3   9   5   5        7  11   7   6
正如您所看到的,数组和的代码被右移,我不太确定如何解决这个问题

    public static void printResult(int [][]array1, int [][]array2, int[][]sum, char arithmetic)
{
if (arithmetic == '+') {
        // Text for two matrices when added
        System.out.print("Here are the two matrices, and the result when added:\n");

        // For loop to print array1 + array2 = sum with format
        for (int i = 0; i < array1.length; i++) {
            // For loop to print out array 1 and add string, if to place +
            for (int j = 0; j < array1[i].length; j++) {
                System.out.printf("%4s", array1[i][j]);     
                if (i == array1.length / 2 && j == array1[i].length-1) {
                    System.out.printf("%4s", '+');
                }
            }
            System.out.print("\t");

            // For loop to print out array2 and equals string, if to place =
            for (int j = 0; j < array1[i].length; j++) {
                System.out.printf("%2s", array2[i][j] + "   ");
                if (i == array1.length / 2 && j == array1[i].length-1) {
                    System.out.printf("%1s", '=');
                }
            }
            System.out.print("  ");

            // For loop to print out sum of array1 + array2
            for (int j = 0; j < array1[i].length; j++) {
                System.out.printf("%4s", sum[i][j]);
            }
            System.out.print("\n");
        }
    }
    else if (arithmetic == '-') {
    }
    else if (arithmetic == '*') {
    }
    else if (arithmetic == '/') {
    }
    else if (arithmetic == '%') {
    }
}

我认为这是为了确保数字在打印时始终占用2个空格:

System.out.printf("%2s", array2[i][j] + "   ");
但是由于添加了(从技术上讲,是串联的)
,因此得到的字符串比
%2s
字段大,因此没有填充短数字


另外,您有代码来决定何时打印
+
&
=
,但每个人都应该在打印符号和打印空格之间做出决定,以保持所有内容对齐。

为什么要标记python?对我来说,这看起来像Java…@MattDMo可能是个错误,但没有注意到。
 Here are the two matrices, and the result when added: 
    2   2   7    3   4   3        5   6   8   
    4   4   8   +        6   8   5   =     7   9  13  
    1   9   3    6   8   6        7   7   
System.out.printf("%2s", array2[i][j] + "   ");