格式化矩阵从左到右读取

时间:2015-04-12 19:02:30

标签: java matrix bluej

我需要我的矩阵看起来完全像这样。

 Here are the two matrices, and the result when added: 
    2   2   7   4        3   4   3   3        5   6  10   7
    4   4   8   8        6   8   5   5       10  12  13  13
    1   9   3   7        6   8   6   9        7  17   9  16
    2   3   2   9   +    4   4   7   1   =    6   7   9  10
    2   9   1   1        9   8   2   5       11  17   3   6
    6   1   8   4        4   8   2   2       10   9  10   6

每个数字应该在输出中使用4个位置,而+和=应该在中间行,但是我不能得到+和=符号来保留较小的数组。我的问题可能出在我的if(i == 3&& j == 3)语句中。 我对这部分的代码如下。

   public static void printResult(int [][]array1, int [][]array2, int[][]sum, char arithmetic)
{
    // Declares 2-dimensional array the same size as one in parameters
    int [][]arraySum = new int [array1.length][array1[0].length];

    // Arithmetic characters to be printed when asked for
    String add = "+";
    String subtract = "-";
    String multiply = "*";
    String divide = "/";
    String remainder = "%";
    String equals = "=";

    // If arithmetic is addition to print matrices and add them to show result
    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 < arraySum.length; i++) {
            // For loop to print out array 1 and add string
            for (int j = 0; j < arraySum[i].length; j++) {
                System.out.printf("%3s", array1[i][j] + " ");     
                if (i == 3 && j == 3) {
                    System.out.printf("%2s", add);
                }
            }
            System.out.print("\t");

            // For loop to print out array2 and equals string
            for (int k = 0; k < arraySum[i].length; k++) {
                System.out.printf("%3s", array2[i][k] + " ");
                if (i == 3 && k == 3) {
                    System.out.printf("%2s", equals);
                }
            }
            System.out.print("\t");

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

我为3x3阵列获得的示例。 (仍应打印+或=)。

5  3  5      6  7  4    11 10  9
8  2  9      1  5  5     9  7 14
9  7  3      2  5  1    11 12  4

4 个答案:

答案 0 :(得分:1)

如果你的矩阵是3x3,那么

ij永远不会是3。如果您希望+=位于中间,请写下以下内容:

if (i == arraySum.length / 2 && j == arraySum[i].length - 1)

答案 1 :(得分:1)

尝试if (i == arraySum.length/2 && j == arraySum[i].length-1)

答案 2 :(得分:0)

if (i == 3 && j == 3) {替换为if (i == (array1 / 2) && j == (array1 / 2)) {

答案 3 :(得分:0)

你的问题是在for循环中,你在第4行(0,1,2,3)添加了加号和等号。如果您只有三行,则不会打印。你可以做些什么来做到这一点:

for (int i = 0; i < arraySum.length; i++) {
    // For loop to print out array 1 and add string
    for (int j = 0; j < arraySum[i].length + 1; j++) {
        System.out.printf("%3s", array1[i][j] + " ");     
        if (i == Math.ceil(arraySum.length/2) && j == arraySum.length) {
            System.out.printf("%2s", add);
        }
    }

它将在阵列的中间(高度)和末尾(纵向)打印标志。要小心,它还使用ceiling函数来舍入到下一个整数。

相关问题