2012年(106)
分类: Java
2012-05-08 17:05:45
import java.util.Scanner;
public class Matrix {
public double[][] create() {
Scanner sc = new Scanner(System.in) ;
System.out.print("请输入矩阵的行高:");
int a = sc.nextInt() ;
System.out.print("请输入矩阵的列宽:");
int b = sc.nextInt() ;
double[][] x = new double[a][b] ;
for(int i=0;ifor(int j=0;jSystem.out.print("请输入元素x["+i+"]["+j+"]的值:" );
x[i][j] = sc.nextDouble() ;
}
}
return x ;
}
public double[][] multiply(double[][] x,double[][] y){
double[][] result = null ;
int a = x[0].length ;
int b = y.length ;
if(a != b){
System.out.println("输入的维数不匹配,不能进行运算");
}else{
int c = x.length ;
int d = y[0].length ;
result = new double[c][d] ;
for(int i=0;i
for(int k=0;ksum += x[i][k]*y[k][j] ;
}
result[i][j] = sum ;
}
}
}
return result ;
}
public void print(double[][] x){
System.out.println("
for(int i=0;i
}
System.out.println();
}
}
}
class TestMatrix {
public static void main(String[] args) {
Matrix m = new Matrix() ;
//double[][] x = {{1,2},{3,2}} ;
//double[][] y = {{1,2,1},{2,3,3}} ;
System.out.println("创建第一个数组:") ;
double[][] x = m.create() ;
m.print(x) ; //用来验证输入的是否和你一致,没什么作用
System.out.println("创建第二个数组:");
double[][] y = m.create() ;
m.print(y) ; //用来验证输入的是否和你一致,没什么作用
double[][] result = m.multiply(x, y) ;
if(result == null){
return ; //如果输入的矩阵不能运算就不输出结果了。
}
m.print(result) ;
}
}