📄 matrixtest.java
字号:
import java.io.*;
class KeyInput{
static InputStreamReader isr=new InputStreamReader(System.in);
static BufferedReader br=new BufferedReader(isr);
public static int readInt(){
int i=0;
try{
i=Integer.parseInt(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
return i;
}
public static float readFloat() {
float f=0.0f;
try{
f=Float.parseFloat(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
return f;
}
public static double readDouble() {
double d=0.0;
try{
d=Double.parseDouble(br.readLine());
}
catch(Exception e){
System.out.println(e);
}
return d;
}
public static String readString() {
String s="";
try{
s=br.readLine();
}
catch(Exception e){
System.out.println(e);
}
return s;
}
public static boolean readBoolean() {
String s="";
try{
s=br.readLine();
}
catch(Exception e){
System.out.println(e);
}
if (s.toUpperCase().equals("TRUE"))
return true;
else
return false;
}
public static char readChar(){
try{
return (char)System.in.read();
}
catch(Exception e){
System.out.println(e);
}
return ' ';
}
}
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
class Matrix
{
protected int rows;
protected int cols;
protected int data[][];
protected boolean trueth;
Matrix()
{
this.cols=0;
this.rows=0;
}
Matrix(int rows,int cols)
{
this.rows=rows;
this.cols=cols;
this.data=new int[rows][cols];
this.trueth=true;
}
Matrix(int rows,int cols,int data[][])
{
this.data=new int[rows][cols];
this.rows=rows;
this.cols=cols;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
setdata(i,j,data[i][j]);
}
}
this.trueth=true;
}
double getdata(int rows,int cols)
{
return data[rows][cols];
}
void setdata(int rows,int cols,int value)
{
this.data[rows][cols]=value;
}
Matrix multiply(Matrix m)
{
Matrix p=new Matrix(this.rows,m.cols);
if(this.cols==m.rows)
{
for(int i=0;i<this.rows;++i)
{
for(int j=0;j<m.cols;++j)
{
p.data[i][j]=0;
for(int k=0;k<this.cols;++k)
{
p.data[i][j]+=this.data[i][k]*m.data[k][j];
}
}
}
}
else
{
p.trueth=false;
}
return p;
}
void toString(Matrix q)
{
for(int a=0;a<q.rows;++a)
{
int num=0;
for(int b=0;b<q.cols;++b)
{
System.out.print(q.data[a][b]);
num++;
if(num==q.cols)
{
System.out.print("\n");
}
else
System.out.print("\t");
}
}
}
}
class MatrixTest
{
public static void main(String arg[])
{
System.out.println("请输入要创建的第一个矩阵的行列数:");
System.out.print("行 rows=");
int row1=KeyInput.readInt();
System.out.print(" 列 cols=");
int col1=KeyInput.readInt();
int s1[][]=new int[row1][col1];
for(int i=0;i<row1;++i)
{
for(int j=0;j<col1;++j)
{
s1[i][j]=(int)(Math.random()*100);
}
}
System.out.println("请输入要创建的第二个矩阵的行列数:");
System.out.print("行 rows=");
int row2=KeyInput.readInt();
System.out.print(" 列 cols=");
int col2=KeyInput.readInt();
int s2[][]=new int[row2][col2];
for(int x=0;x<row2;++x)
{
for(int y=0;y<col2;++y)
{
s2[x][y]=(int)(Math.random()*100);
}
}
Matrix m1=new Matrix();
m1=new Matrix(row1,col1,s1);
Matrix m2=new Matrix();
m2=new Matrix(row2,col2,s2);
Matrix m3=new Matrix();
System.out.println("创建的第一个矩阵为:");
m1.toString(m1);
System.out.println("创建的第二个矩阵为:");
m2.toString(m2);
m3=m1.multiply(m2);
if(m3.trueth)
{
System.out.println("两个矩阵对象m1和m2的乘积为:");
m3.toString(m3);
}
else
System.out.println("两个矩阵无法相乘!!!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -