以下是我编写的Matrix类,但是在编译时总是提示Multipy(Matrix)方法中有错误,请帮忙纠正下
public class Matrix
{
int rows,cols;
double data[][]; public Matrix(){
} public Matrix(int rows,int cols){
this.rows=rows;
this.cols=cols;
data=new double[rows][cols];
} public Matrix(int rows,int cols,double data[][]){
this.rows=rows;
this.cols=cols;
this.data=data;
} public double getData(int row,int col){
return data[row][col];
}
public double setData(int row,int col,double value){
data[row][col]=value;
} public double multiply(Matrix m){
if(this.cols!=m.rows)
System.out.println("这两个矩阵不能相乘");
else{
Matrix ma = new Matrix(rows,m.cols);
for(int i=0;i<data.length;i++)
for(int j=0;j<m.data[0].length;j++)
for(int k=0;k<data[0].length;k++)
ma.data[i][j] += data[i][k]*m.data[k][j];
return ma;
}
} /* public static void main(String args[]){
}*/
}
public class Matrix
{
int rows,cols;
double data[][]; public Matrix(){
} public Matrix(int rows,int cols){
this.rows=rows;
this.cols=cols;
data=new double[rows][cols];
} public Matrix(int rows,int cols,double data[][]){
this.rows=rows;
this.cols=cols;
this.data=data;
} public double getData(int row,int col){
return data[row][col];
}
public double setData(int row,int col,double value){
data[row][col]=value;
} public double multiply(Matrix m){
if(this.cols!=m.rows)
System.out.println("这两个矩阵不能相乘");
else{
Matrix ma = new Matrix(rows,m.cols);
for(int i=0;i<data.length;i++)
for(int j=0;j<m.data[0].length;j++)
for(int k=0;k<data[0].length;k++)
ma.data[i][j] += data[i][k]*m.data[k][j];
return ma;
}
} /* public static void main(String args[]){
}*/
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货