在java中执行矩阵运算的函数出错

m1m5dgzv  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(205)

代码如下:

import java.io.*;
public class MatrixOperations {
  public static void main(String[] args) {
    DataInputStream in =new DataInputStream(System. in );
    int a[][] = null;
    int b[][] = null;
    int c[][] = null;
    int i,
    j,
    k,
    r1,
    c1,
    r2,
    c2,
    ch;
    try {

      System.out.println("\nEnter the number of rows and columns of the 
1st matrix :");
      r1 = Integer.parseInt( in .readLine());
      c1 = Integer.parseInt( in .readLine());
      a = new int[r1][c1];
      System.out.println("\nEnter the elements of the 2st matrix");
      for (i = 0; i < r1; i++)
      for (j = 0; j < c1; j++)
      a[i][j] = Integer.parseInt( in .readLine());
      System.out.println("\nEnter the number of rows and columns of the
2nd matrix :");
      r2 = Integer.parseInt( in .readLine());
      c2 = Integer.parseInt( in .readLine());
      System.out.println("\nEnter the elements of the 2nd matrix");
      b = new int[r2][c2];
      for (i = 0; i < r2; i++)
      for (j = 0; j < c2; j++)
      b[i][j] = Integer.parseInt( in .readLine());
      System.out.println("\nThe 1st matrix is:");
      for (i = 0; i < r1; i++) {
        for (j = 0; j < c1; j++)
        System.out.print(a[i][j] + " ");
        System.out.print("\n");
      }
      System.out.println("\nThe 2nd matrix is:");
      for (i = 0; i < r2; i++) {
        for (j = 0; j < c2; j++)
        System.out.print(b[i][j] + " ");
        System.out.print("\n");
      }
      System.out.println("\n1.Addition \n2.Multiplication");
      System.out.println("\nEnter the operation you want to perform :");
      ch = Integer.parseInt( in .readLine());
      if (ch == 1) {
        if (r1 == r2 && c1 == c2) {
          c = new int[r1][c1];
          System.out.println("\nSum of the matrices :");
          for (i = 0; i < r1; i++) {
            System.out.print("\n");
            for (j = 0; j < c1; j++) {
              c[i][j] = a[i][j] + b[i][j];
              System.out.print(c[i][j] + " ");
            }
          }
        }
        else System.out.println("Addition not possible!!!");
      }
      else if (ch == 2) {
        if (c1 == r2) {
          c = new int[r1][c2];
          System.out.println("Product of the matrices :");
          for (i = 0; i < r1; i++) {
            for (j = 0; j < c2; j++) {
              c[i][j] = 0;
              for (k = 0; k < c1; k++)
              c[i][j] = c[i][j] + a[i][k] * b[k][j];
              System.out.print(c[i][j] + " ");
            }
            System.out.print("\n");
          }
        }
        else System.out.println("Multiplication not possible!!!!");
      }
      else System.out.println("Invalid option!!!");
    }
    catch(Exception e) {
      System.out.println("Error : " + e);
    }
  }
}

下面是我得到的错误https://imgur.com/a/ddyja3u
我真的不知道怎么修理它们。我尝试过很多随机的事情,比如改变变量或者删除某些东西,来找出错误发生的地方,但是到目前为止什么都不起作用。我希望你们中的一个能指出问题并发送正确的代码。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题