【问题标题】:How do I insert "-" and "=" when printing 2D arrays打印二维数组时如何插入“-”和“=”
【发布时间】:2016-03-29 08:46:35
【问题描述】:

我是 Java 编程的新手。我编写了一种方法,可以从另一个矩阵中减去一个 3×3 矩阵。我无法分别在第一个和第二个矩阵之后插入“-”和“=”。

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;
    }
}

谢谢大家!

【问题讨论】:

    标签: java arrays matrix


    【解决方案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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 1970-01-01
      • 2020-11-17
      • 2021-12-11
      • 2015-02-03
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多