繁体   English   中英

打印2D数组时如何插入“ - ”和“=”

[英]How do I insert “-” and “=” when printing 2D arrays

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

谢谢你们!

它可能有帮助!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM