2015-12-23 53 views
3

我是Java編程的新手。我寫了一個從另一個減去3乘3矩陣的方法。我無法分別在第一個和第二個矩陣之後插入「 - 」和「=」。如何在打印2D陣列時插入「 - 」和「=」

My desired output is: 

1.0 2.0 3.0   1.0 0.5 1.0   0.0 1.5 2.0 
4.0 5.0 6.0 -  2.5 3.0 2.5 = 1.5 2.0 3.5 
7.0 8.0 9.0   5.0 6.5 7.0   2.0 1.5 2.0 

However, the closest I have got has been: 

1.0 2.0 3.0   1.0 0.5 1.0  0.0 1.5 2.0 
4.0 5.0 6.0   2.5 3.0 2.5  1.5 2.0 3.5 
7.0 8.0 9.0   5.0 6.5 7.0  2.0 1.5 2.0 

請看以下程序:

import java.util.Scanner; 
public class MatrixMagic { 
public static void main(String[] args) { 
    // Obtain data for first matrix 
    Scanner input = new Scanner(System.in); 
    System.out.print("Enter matrix1: "); 
    double[][] matrix1 = new double[3][3]; 
    for (int i = 0; i < matrix1.length; i++){ 
     for (int j = 0; j < matrix1[i].length; j++) 
     matrix1[i][j] = input.nextDouble(); 
    } 

    // Obtain data for second matrix 
    System.out.print("Enter matrix2: "); 
    double[][] matrix2 = new double[3][3]; 
    for(int i = 0; i < matrix2.length; i++){ 
     for (int j = 0; j < matrix2[i].length; j++) 
     matrix2[i][j] = input.nextDouble(); 
    } 

    // Returns result of subtraction 
    double[][] subtractResult = subtractMatrices(matrix1, matrix2);  

    System.out.println("\nThe matrices are subtracted as follows: "); 

    // Print first matrix 
    for (int i = 0; i < 3; i++){ 

     for (int j = 0; j < 3; j++){ 
     System.out.print(matrix1[i][j] + "\t"); 
    } 
    System.out.print("   "); 

     // Print second matrix 
     for (int j = 0; j < 3; j++){ 
    System.out.print(matrix2[i][j] + "\t"); 
    } 
    System.out.print("   "); 

    // Print resultant matrix 
    for (int j = 0; j < 3; j++){ 
    System.out.print(subtractResult[i][j] + "\t"); 
    } 
    System.out.println(); // Prints line after each row 
    } 
} 

/** Method subtracts one matrix from another */  
public static double[][] subtractMatrices(double[][] a, double[][] b){ 
double[][] subtractResult = new double[3][3]; 

    for(int i = 0; i < subtractResult.length; i++) 
     for (int j = 0; j < subtractResult[i].length; j++){ 
     subtractResult[i][j] = a[i][j] - b[i][j]; 
    }  

     return subtractResult; 
    } 
} 

謝謝大家!

回答

1

它可能會有幫助!

import java.util.Scanner; 

    public class MatrixMagic { 
     public static void main(String[] args) { 
      // Obtain data for first matrix 
      Scanner input = new Scanner(System.in); 
      System.out.print("Enter matrix1: "); 
      double[][] matrix1 = new double[3][3]; 
      for (int i = 0; i < matrix1.length; i++) { 
       for (int j = 0; j < matrix1[i].length; j++) 
        matrix1[i][j] = input.nextDouble(); 
      } 

      // Obtain data for second matrix 
      System.out.print("Enter matrix2: "); 
      double[][] matrix2 = new double[3][3]; 
      for (int i = 0; i < matrix2.length; i++) { 
       for (int j = 0; j < matrix2[i].length; j++) 
        matrix2[i][j] = input.nextDouble(); 
      } 

      // Returns result of subtraction 
      double[][] subtractResult = subtractMatrices(matrix1, matrix2); 

      System.out.println("\nThe matrices are subtracted as follows: "); 

      // Print first matrix 
      for (int i = 0; i < 3; i++) { 

       for (int j = 0; j < 3; j++) { 
        System.out.print(matrix1[i][j] + "\t"); 
       } 
       if (i == 1) { 
        System.out.print(" -  "); 
       } else { 
        System.out.print("   "); 
       } 

       // Print second matrix 
       for (int j = 0; j < 3; j++) { 
        System.out.print(matrix2[i][j] + "\t"); 
       } 
       if (i == 1) { 
        System.out.print(" =  "); 
       } else { 
        System.out.print("   "); 
       } 

       // Print resultant matrix 
       for (int j = 0; j < 3; j++) { 
        System.out.print(subtractResult[i][j] + "\t"); 
       } 
       System.out.println(); // Prints line after each row 
      } 
     } 

     /** Method subtracts one matrix from another */ 
     public static double[][] subtractMatrices(double[][] a, double[][] b) { 
      double[][] subtractResult = new double[3][3]; 

      for (int i = 0; i < subtractResult.length; i++) 
       for (int j = 0; j < subtractResult[i].length; j++) { 
        subtractResult[i][j] = a[i][j] - b[i][j]; 
       } 

      return subtractResult; 
     } 
    } 
+0

這真的很有幫助!非常感謝! –