我的代码有一个问题,编译说 m1.cinMatrix(a [][]);这里错误放置了构造,我不明白,这并不是构造函数呀。cinMatrix是数组赋值的函数,我在输入之后,我觉得就应该调用它来给对象的数组变量赋值的。望大家帮忙。import java.io.*;
class Matrix
{
Matrix(int r,int c)
{
row = r;
column = c;
array = new int[row][column];
}
public void cinMatrix(int[][] a) //输入矩阵
{
for(int i = 0; i<a.length; i++ )
for(int j = 0; j<a.length; j++)
{
array[i][j] = a[i][j];
}
}
public Matrix add (Matrix a) //矩阵加法
{
if(row != a.row||column != a.column)
{
System.out.println("请输入同型矩阵!");
return null;
}
else
{
Matrix c = new Matrix(row,column);
for(int i = 0; i<row; i++ )
for(int j = 0; j<column; j++)
c.array[i][j] = array[i][j] + a.array[i][j];
return c;
}
}
public Matrix minus (Matrix a) //矩阵减法
{
if(a.row != row||a.column != 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] = array[i][j] - a.array[i][j];
return c;
}
}
public Matrix multiply(Matrix a) //矩阵乘法
{
if(a.column!=row)
{
System.out.println("请输入同型矩阵!");
return null;
}
else
{
Matrix c = new Matrix(a.row,a.column);
for(int i = 0; i<row; i++ )
for(int j = 0; j<a.column; j++)
{
c.array[i][j] = 0;
for(int k=0;k<column;k++)
c.array[i][j]+=array[i][k]*a.array[k][j];
}
return c;
}
}
public void print(Matrix a) //矩阵输出 {
for(int i=0;i<a.row;i++)
{
for(int j=0;j<a.column;j++)
{
System.out.println(" a["+i+"]["+j+"]="+a.array[i][j]);
}
}
}
int row;
int column;
int[][] array;
}public class MyMatrix
{
public static void main(String[] args) throws Exception
{
int r1=0,c1=0;
int r2=0,c2=0; System.out.println("Input the row of the first matrix:");
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
r1= Integer.parseInt(br.readLine());
System.out.println("Input the column of the first matrix:");
c1 = Integer.parseInt(br.readLine());
System.out.println("Input the first matrix:");
Matrix m1=new Matrix(r1,c1);
int[][] a= new int [r1][c1];
for(int i=0;i<m1.row;i++)
{
String[] t = br.readLine().split(" ");
for(int j=0;j<m1.column;j++)
{
a[i][j] = Integer.parseInt(t[j]);
}
}
m1.cinMatrix(a [][]);
System.out.println("Input the row of the second matrix:");
r2= Integer.parseInt(br.readLine());
System.out.println("Input the column of the second matrix:");
c2 = Integer.parseInt(br.readLine());
System.out.println("Input the second matrix:");
Matrix m2=new Matrix(r2,c2);
for(int i=0;i<m2.row;i++)
{
String[] t = br.readLine().split(" ");
for(int j=0;j<m2.column;j++)
{
a[i][j] = Integer.parseInt(t[j]);
}
}
m2.cinMatrix(a[][]);
Matrix m3 = m1.add(m2);
Matrix m4 = m1.minus(m2);
Matrix m5 = m1.multiply(m2); System.out.println("求的矩阵的和为:");
m3.print(m3);
System.out.println();
System.out.println("求的矩阵的差为:");
m4.print(m4);
System.out.println();
System.out.println("求的矩阵的积为:");
m5.print(m5);
}
}
class Matrix
{
Matrix(int r,int c)
{
row = r;
column = c;
array = new int[row][column];
}
public void cinMatrix(int[][] a) //输入矩阵
{
for(int i = 0; i<a.length; i++ )
for(int j = 0; j<a.length; j++)
{
array[i][j] = a[i][j];
}
}
public Matrix add (Matrix a) //矩阵加法
{
if(row != a.row||column != a.column)
{
System.out.println("请输入同型矩阵!");
return null;
}
else
{
Matrix c = new Matrix(row,column);
for(int i = 0; i<row; i++ )
for(int j = 0; j<column; j++)
c.array[i][j] = array[i][j] + a.array[i][j];
return c;
}
}
public Matrix minus (Matrix a) //矩阵减法
{
if(a.row != row||a.column != 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] = array[i][j] - a.array[i][j];
return c;
}
}
public Matrix multiply(Matrix a) //矩阵乘法
{
if(a.column!=row)
{
System.out.println("请输入同型矩阵!");
return null;
}
else
{
Matrix c = new Matrix(a.row,a.column);
for(int i = 0; i<row; i++ )
for(int j = 0; j<a.column; j++)
{
c.array[i][j] = 0;
for(int k=0;k<column;k++)
c.array[i][j]+=array[i][k]*a.array[k][j];
}
return c;
}
}
public void print(Matrix a) //矩阵输出 {
for(int i=0;i<a.row;i++)
{
for(int j=0;j<a.column;j++)
{
System.out.println(" a["+i+"]["+j+"]="+a.array[i][j]);
}
}
}
int row;
int column;
int[][] array;
}public class MyMatrix
{
public static void main(String[] args) throws Exception
{
int r1=0,c1=0;
int r2=0,c2=0; System.out.println("Input the row of the first matrix:");
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
r1= Integer.parseInt(br.readLine());
System.out.println("Input the column of the first matrix:");
c1 = Integer.parseInt(br.readLine());
System.out.println("Input the first matrix:");
Matrix m1=new Matrix(r1,c1);
int[][] a= new int [r1][c1];
for(int i=0;i<m1.row;i++)
{
String[] t = br.readLine().split(" ");
for(int j=0;j<m1.column;j++)
{
a[i][j] = Integer.parseInt(t[j]);
}
}
m1.cinMatrix(a [][]);
System.out.println("Input the row of the second matrix:");
r2= Integer.parseInt(br.readLine());
System.out.println("Input the column of the second matrix:");
c2 = Integer.parseInt(br.readLine());
System.out.println("Input the second matrix:");
Matrix m2=new Matrix(r2,c2);
for(int i=0;i<m2.row;i++)
{
String[] t = br.readLine().split(" ");
for(int j=0;j<m2.column;j++)
{
a[i][j] = Integer.parseInt(t[j]);
}
}
m2.cinMatrix(a[][]);
Matrix m3 = m1.add(m2);
Matrix m4 = m1.minus(m2);
Matrix m5 = m1.multiply(m2); System.out.println("求的矩阵的和为:");
m3.print(m3);
System.out.println();
System.out.println("求的矩阵的差为:");
m4.print(m4);
System.out.println();
System.out.println("求的矩阵的积为:");
m5.print(m5);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货