2012-12-04 212 views
-1

该程序添加了两个3x3矩阵。它编译和运行,但输出,而不是:Java矩阵阵列

1.0 2.0 3.0  0.0 2.0 4.0  1.0 4.0 7.0 
4.0 5.0 6.0 + 1.0 4.5 2.2 = 5.0 9.5 8.2 
7.0 8.0 9.0  1.1 4.3 5.2  8.1 12.3 14.2 

它产生:

1.0 2.0 3.0  0.0 2.0 4.0  0.0 0.0 0.0 
4.0 5.0 6.0 + 1.0 4.5 2.2 = 0.0 0.0 0.0 
7.0 8.0 9.0  1.1 4.3 5.2  0.0 0.0 0.0 

我不知道为什么输出显示为全零?程序中的数学“似乎”对我来说......我在这里错过了什么吗?

import java.util.Scanner; 

public class AddMatrices{ 

public static void main(String[] args){ 

    Scanner input = new Scanner(System.in); 

    int N = 3; 

    //get the users input and store it in the two arrays 
    System.out.println("\nEnter matrix1: \n"); 

    //declare 2 arrays with the appropriate number of rows and columns in 
    //them to store the numbers in each matrix. 
    //this is the first one. 
    double[][] matrix1 = new double[N][N]; 

    for (int i = 0; i < matrix1.length; i++) { 
     for (int j = 0; j < matrix1[i].length; j++) { 
      matrix1[i][j] = input.nextDouble(); 
     } 
    } 

    //get the users input and store it in the two arrays 
    System.out.println("\nEnter matrix2: \n"); 

    //declare 2 arrays with the appropriate number of rows and columns in 
    //them to store the numbers in each matrix. 
    //this is the second one. 
    double[][] matrix2 = new double[3][3]; 

    for (int i = 0; i < matrix1.length; i++) { 
     for (int j = 0; j < matrix1[i].length; j++) { 
      matrix2[i][j] = input.nextDouble(); 
     } 
    } 


    //call the addMatrix method and pass it the two arrays 
    double[][] resultingMatrix = addMatrix(matrix1, matrix2); 
    System.out.println("The addition of the matrices is "); 



}//end of main method 


//write the addMatrix method to add the two matrices and display the result 

public static double[][] addMatrix(double[][] m1, double[][] m2){ 

    double[][] result = new double[m1.length][m1[0].length]; 

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

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

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

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

     if (i == m1.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 
+1

区分逻辑与演示。首先将矩阵添加到内存中的新矩阵中,稍后打印结果。将使调试代码变得更容易。哦,一个调试器将允许你关注你的逻辑是如何工作的。 – SJuan76

回答

1

我想你永远不会将结果分配到不同的结果

你应该改变

m1[i][j] += m2[i][j]; 

result[i][j]=m1[i][j] + m2[i][j]; 
+0

工作正常!非常感谢! :) – user1368970

2

您正在将附加值设置为m1而不是结果。在你的第一双for循环,只是做:

result[i][j] = m1[i][j]+m2[i][j]; 
+1

+1。我只是写同样的东西,你快枪=) – Juvanis

+0

pshew pshew! =) – awolfe91

+0

是的,这现在更有意义。谢谢! – user1368970