2012-12-04 3 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로 설정합니다. 첫 번째 두 번에 대한 루프에서 바로 수행

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

+1. 나는 똑같은 것을 쓰고 있었는데, 더 빠른 총 =) – Juvanis

+0

pshew pshew! =) – awolfe91

+0

네, 이제 훨씬 더 의미가 있습니다. 고맙습니다! – user1368970