乘以2个矩阵 - 输出问题

时间:2014-01-17 02:30:06

标签: java matrix multiplication

我正在编写一个使用2D数组乘以2个矩阵的程序。然后结果应以矩形打印。这是代码:

import java.util.Scanner;
public class MatrixMath   
{



public static void main(String[] args) {
   int m, n, p, q, c, d, k;
   int sum1 = 0;

   Scanner scan = new Scanner(System.in);  

   System.out.println("Please enter the number of rows and columns for the first       matrix."); 
      m = scan.nextInt();
      n = scan.nextInt();

   int first[][] = new int[m][n];

        System.out.println("Enter the elements of first matrix:"); 

        for ( c = 0 ; c < m ; c++ ) 
           for ( d = 0 ; d < n ; d++ )
              first[c][d] = scan.nextInt();

   System.out.println("Please enter the number of rows and columns for the second matrix."); 
      p = scan.nextInt();
      q = scan.nextInt();


     int second[][] = new int[p][q];
     int multiply[][] = new int[m][q];

     System.out.println("Enter the elements of second matrix:"); 

     for ( c = 0 ; c < p ; c++ )   
        for ( d = 0 ; d < q ; d++ )
           second[c][d] = scan.nextInt();


     for ( c = 0 ; c < m ; c++ )  
     {
        for ( d = 0 ; d < q ; d++ )
        {   
           for ( k = 0 ; k < p ; k++ )
           {
           sum1 = sum1 + first[c][k]*second[k][d]; 
           }

           multiply[c][d] = sum1;

           sum1 = 0;
           }
        }

           System.out.println("The product of the two matrices is: ");  

              for ( c = 0 ; c < m ; c++ )
              {
                 for ( d = 0 ; d < q ; d++ )
                 System.out.print(multiply[c][d]+"\t");

                 System.out.print("\n");


           int sum[][] = new int[m][n];  // Sum is calculated.

           for ( c = 0 ; c < m ; c++ )  
                 for ( d = 0 ; d < n ; d++ )




     System.out.println("The sum of the two matrices is: "); 
     for ( c = 0 ; c < m ; c++ )
           {
           for ( d = 0 ; d < n ; d++ )
           System.out.print(sum[c][d]+"\t");

           System.out.println();

   }
  }
 }
}

用户输入值后的输出问题:

两个矩阵的乘积是:
8 8
两个矩阵的总和是:
两个矩阵的总和是:
两个矩阵的总和是:
两个矩阵的总和是:
0 0
0 0

我希望将总和打印为矩形,以及产品...但只有一半的产品似乎是打印而且总和输出有问题。感谢帮助。非常感谢。

2 个答案:

答案 0 :(得分:1)

就个人而言,我明确地为所有for循环和if语句添加大括号。你已经忘记了其中几个,这就是你的问题所在。你的外循环有控制变量'c' - 但是因为你忘了关闭括号,你在内循环中重复使用'c'。

此外,您的缩进遍布各处,因此模糊了问题。在您的IDE中,找到自动缩进功能 - 它是您的朋友,并可能会为您突出显示它。

所以,你应该:

for ( c = 0 ; c < m ; c++ ) {
    for ( d = 0 ; d < q ; d++ ) {
       System.out.print(multiply[c][d]+"\t");
    }

    System.out.print("\n");

} // missed this one

int sum[][] = new int[m][n];  // Sum is calculated.

for ( c = 0 ; c < m ; c++ )   {
    for ( d = 0 ; d < n ; d++ ) {

        System.out.println("The sum of the two matrices is: "); 

答案 1 :(得分:1)

尝试正确缩进代码以避免混淆。

完整的代码在这里:

import java.util.Scanner;
public class MatrixMath      
{



 public static void main(String[] args) {
    int m, n, p, q, c, d, k;
    int sum1 = 0;

    Scanner scan = new Scanner(System.in);  

    System.out.println("Please enter the number of rows and columns for the first       matrix."); 
      m = scan.nextInt();
      n = scan.nextInt();

    int first[][] = new int[m][n];

    System.out.println("Enter the elements of first matrix:"); 

    for ( c = 0 ; c < m ; c++ ) 
       for ( d = 0 ; d < n ; d++ )
          first[c][d] = scan.nextInt();

    System.out.println("Please enter the number of rows and columns for the second matrix."); 
    p = scan.nextInt();
    q = scan.nextInt();


     int second[][] = new int[p][q];
     int multiply[][] = new int[m][q];

     System.out.println("Enter the elements of second matrix:"); 

     for ( c = 0 ; c < p ; c++ )   
        for ( d = 0 ; d < q ; d++ )
           second[c][d] = scan.nextInt();


     for ( c = 0 ; c < m ; c++ )  
     {
        for ( d = 0 ; d < q ; d++ )
        {   
           for ( k = 0 ; k < p ; k++ )
           {
               sum1 = sum1 + first[c][k]*second[k][d]; 
           }
           multiply[c][d] = sum1;
           sum1 = 0;
         }
      }

       System.out.println("The product of the two matrices is: ");  

          for ( c = 0 ; c < m ; c++ )
          {
             for ( d = 0 ; d < q ; d++ )
             {
                 System.out.print(multiply[c][d]+"\t");
             }
             System.out.print("\n");
          }
       int sum[][] = new int[m][n];  // Sum is calculated.

       for ( c = 0 ; c < m ; c++ )
       {
           for ( d = 0 ; d < n ; d++ )
           {
               sum[c][d] = first[c][d] + second[c][d];
           }
       }



     System.out.println("The sum of the two matrices is: "); 
     for ( c = 0 ; c < m ; c++ )
     {
           for ( d = 0 ; d < n ; d++ )
           {
               System.out.print(sum[c][d]+"\t");
           }
           System.out.println();
      }
  }
 }