无法获得输出的添加

时间:2014-06-23 04:34:32

标签: java

一切都很好,但我不能再出现这个了吗?当我运行程序时,在添加矩阵部分时它是空白的。提前致谢。 BtW有谁知道如何让这个显示右列合理?

public static void displayMatrixes(int[][] matrix1, int[][] matrix2, int[][] resultsMatrix) {
     System.out.println("This is how i want it to output");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                 System.out.print(matrix1[i][j] + " ");
             }
                  System.out.print("+ ");
                  for (int j = 0; j < 3; j++) {
                    System.out.print(matrix2[i][j]+ " ");
            }
            System.out.print("= ");
            for (int j = 0; j < 3; j++) {
                System.out.print(resultsMatrix[i][j]+ " ");
            }
                 System.out.println();
        }

        )

这是我的代码

import java.util.Scanner;
public class MatrixAdd
{

   public static void main(String arg[])
  {
       Scanner input = new Scanner(System.in);
       int a[][]= new int[3][3];
       int b[][] = new int[3][3];

       int row, column;

    System.out.println("\nEnter Matrix A:  \n");
     for (int i = 0 ; i < 3 ; i++){
        for (int j = 0; j<3 ; j++){
                a[i][j] = input.nextInt();
            }

      }
          System.out.println("\nEnter Matrix B: \n");
         for (int i = 0; i < 3; i++){
                for (int j = 0; j < 3; j++){
                    b[i][j] = input.nextInt();
                }

      }
        System.out.println("\nMatrix A + Matrix B = Matrix C: \n");

        int[][] resultingMatrix = addMatrix(a, b);

    }



    public static int[][] addMatrix(int[][] a, int[][] b){

        int[][] result = new int[a.length][a[0].length];

        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[0].length; j++){
            result[i][j]=a[i][j] + b[i][j];
            }
        }

        for (int i = 0; i < a.length; i++) {
            char plus = '+';
            for (int j = 0; j < a[0].length; j++) {
                System.out.print("  " + a[i][j]);
            }

            if (i == a.length / 2)
                System.out.print("  " + plus + "  ");
            else {
                System.out.print("  ");
            }

            for (int j = 0; j < b[0].length; j++) {
                System.out.print("  " + b[i][j]);
            }

            if (i == a.length / 2)
                System.out.print("  =  ");
            else {
                System.out.print("  ");
            }

            for (int j = 0; j < result[0].length; j++) {
                System.out.print("  " + "  " + result[i][j]);
            }
            System.out.println();
        }
    return result;
    }//end of add matrices

}//end of class

2 个答案:

答案 0 :(得分:2)

我怀疑给定的程序是否编译。在这一行:int[][] resultsMatrix = displayMatrixes(a, b);您期望int[][],但在您的方法displayMatrixes中,您没有返回任何内容。你也期待第三个参数,你没有通过。

此外,displayMatrixes方法没有返回值,因为你最后要返回一些东西,你必须拥有。再试一次:

    public static void main(String arg[]) {
    Scanner input = new Scanner(System.in);
    int a[][] = new int[3][3];
    int b[][] = new int[3][3];

    int row, column;

    System.out.println("\nEnter Matrix A:  \n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            a[i][j] = input.nextInt();
        }

    }
    System.out.println("\nEnter Matrix B: \n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            b[i][j] = input.nextInt();
        }

    }
    System.out.println("\nMatrix A + Matrix B = Matrix C: \n");

    displayMatrixes(a, b);        

}

public static void displayMatrixes(int[][] a, int[][] b) {

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            System.out.print(a[i][j] + " ");
        }
        System.out.print("+ ");
        for (int j = 0; j < 3; j++) {
            System.out.print(b[i][j] + " ");
        }
        System.out.print("= ");
        for (int j = 0; j < 3; j++) {
            System.out.print((a[i][j] + b[i][j]) + " ");
        }
        System.out.println();
    }        
}

}

答案 1 :(得分:-2)

printf with“%3d”说明符表示最小宽度为三个空格,默认情况下为右对齐。

您可以在http://alvinalexander.com/programming/printf-format-cheat-sheet

处找到正确的对齐问题