2015-11-27 4 views
0

Я написал эту программу, чтобы добавить две матрицы 3x3; может кто-то сказать мне, почему мой выход дает 2 результирующих матрицы ???Результат не отображается правильно для результирующей матрицы

package programExam; 
import java.io.*; 
import java.util.*; 
import java.io.File.*; 
import java.util.Scanner; 

public class progEx { 
    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 array 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"); 


     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 
     System.out.println("The addition of the matrices is: "); 

     double[][] resultingMatrix = addMatrix(matrix1, matrix2); 



    }//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++){ 
       result[i][j]= m1[i][j] += m2[i][j]; 
      } 
     } 

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

      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 
    } 

ВЫВОД:

Enter matrix1: 

4 5 1 
4 3 2 
6 3 9 

Enter matrix2: 

9 2 4 
7 4 2 
1 7 5 

The addition of the matrices is: 

13.0 7.0 5.0 13.0 7.0 5.0 
11.0 7.0 4.0 11.0 7.0 4.0 
7.0 10.0 14.0 7.0 10.0 14.0 

ответ

1

Поскольку у вас есть два System.out.print команды в вашей addMatrix() функции.

Я считаю, что этот код:

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

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

Должно быть просто:

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

Кроме того, обратите внимание, что вы изменяете матрицу m1 в той же функции (непреднамеренно, я считаю). Мне кажется, что эта линия:

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

Должно быть

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

(обратите внимание, что += был заменен +)

+0

спасибо, что неподвижную его; musthe был на трещине – aEX

+0

@aEX в этом случае, пожалуйста, отметьте ответ как принято. –

+0

Я принял его; жаль, что я очень новичок в этом @ Andre Neves – aEX