2012-12-04 209 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