该程序实现矩阵的加减乘,编译的时候没错了,可是运行的时候出错,麻烦帮忙!
import java.io.*;
class Matrix
{
 int row;
 int column;
 int[][] array;
public Matrix(int r,int c)
{
row = r;
column = c;
array = new int[row][column];
}

public  static void cinMatrix(Matrix a) throws Exception //输入矩阵
{
try
{
for(int i = 0; i<a.row; i++ )
for(int j = 0; j<a.column; j++)
{
        BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
        a.array[i][j] = Integer.parseInt(br.readLine());
        
}
}catch(IOException ex){}    
}

public static Matrix  add (Matrix a,Matrix b)
{
if(a.row!=b.row||a.column!=b.column)
{
System.out.println("请输入同型矩阵!");
return null ;
}
else
{
Matrix c = new Matrix(a.row,a.column);
for(int i = 0; i<a.row; i++ )
for(int j = 0; j<a.column; j++)
c.array[i][j] = a.array[i][j] + b.array[i][j];
return c;
}
}

public static Matrix minus (Matrix a,Matrix b)
{
if(a.row!=b.row||a.column!=b.column)
{
System.out.println("请输入同型矩阵!");
return null;
}
else
{
Matrix c = new Matrix(a.row,a.column);
for(int i = 0; i<a.row; i++ )
for(int j = 0; j<a.column; j++)
c.array[i][j] = a.array[i][j] + b.array[i][j];
return c;
}
}

public static Matrix multiply(Matrix a,Matrix b)
{
if(a.column!=b.row)
{
System.out.println("请输入m*n和n*l型的矩阵!");
return null;
}
else
{
Matrix c = new Matrix(a.row,a.column);
for(int i = 0; i<a.row; i++ )
for(int j = 0; j<b.column; j++)
{
c.array[i][j] = 0;
for(int k=0;k<a.column;k++)
c.array[i][j]+=a.array[i][k]*b.array[k][j];
}
return c;
}
}
public static void print(Matrix a) {
for(int i=0;i<a.row;i++)
{
for(int j=0;j<a.column;j++)
{
System.out.println(a.array[i][j]+" ");
}
     }
}
}public class MyMatrix 
{

public static void main(String[] args) throws Exception
{     int r=0,c=0;
    Matrix m1=new Matrix(r,c);
    Matrix m2=new Matrix(r,c);             System.out.println("输入矩阵的行数:");
    BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
    m1.row = Integer.parseInt(br.readLine());
    

    System.out.println("输入矩阵的列数:");
    m1.column = Integer.parseInt(br.readLine());
    
    System.out.println("输入矩阵的行数:");
    m2.row = Integer.parseInt(br.readLine());
    
    System.out.println("输入矩阵的列数:");
    m2.column = Integer.parseInt(br.readLine());
   
    System.out.println("输入矩阵:");
    Matrix.cinMatrix(m1);
    System.out.println("输入矩阵:");
    Matrix.cinMatrix(m2);
    
    
    Matrix m3 = Matrix.add(m1,m2); 
             Matrix m4 = Matrix.minus(m1,m2);  
    Matrix m5 = Matrix.multiply(m1,m2);     System.out.println("求的矩阵的和为:");
    Matrix.print(m3);     
    System.out.println();
    
    System.out.println("求的矩阵的差为:");
    Matrix.print(m4);
    System.out.println();
    
    System.out.println("求的矩阵的积为:");
    Matrix.print(m5);
}
}

解决方案 »

  1.   

    int r = 0, c = 0;
    Matrix m1 = new Matrix(r, c);
    Matrix m2 = new Matrix(r, c);public Matrix(int r, int c) {
    row = r;
    column = c;
    array = new int[row][column];
    }这两段代码,可以看到你生成了两个长度为0的二维矩阵
    而接下来,你输入了两个矩阵的长度,并没有改变已经生成的二维矩阵的列与宽
    所以再接下来,你用a.array[i][j] = Integer.parseInt(br.readLine());
    时就会出现ArrayIndexOutOfBoundsException
      

  2.   

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Matrix.cinMatrix(MyMatrix.java:23)
    at MyMatrix.main(MyMatrix.java:121)
    数组下标越界,应该是cinMatrix(Matrix a)中出了问题!
      

  3.   

    呵呵,看到3楼的想法,我就把部分代码改了:
         int r1=0,c1=0; 
     int r2=0,c2=0;    System.out.println("输入矩阵的行数:"); 
        BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); 
        r1= Integer.parseInt(br.readLine()); 
            System.out.println("输入矩阵的列数:"); 
        c1 = Integer.parseInt(br.readLine()); 
        
        System.out.println("输入矩阵的行数:"); 
        r2 = Integer.parseInt(br.readLine()); 
        
        System.out.println("输入矩阵的列数:"); 
        c2 = Integer.parseInt(br.readLine()); 
      
        Matrix m1=new Matrix(r1,c1); 
        Matrix m2=new Matrix(r2,c2);