import java.util.Random;
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = 0;
int col = 0;
System.out.println("数组行长度:");
row = sc.nextInt();
System.out.println("数组列长度:");
col = sc.nextInt();
double [][] a = new double[row][col];
double [][] b = new double[row][col];
//二维数组随机赋值
getArr(a);
getArr(b);
//打印二维数组
System.out.println("a的二维数组:");
showArr(a);
System.out.println("b的二维数组:");
showArr(b);
double [][] arr = addMatrix(a,b);
//二维数组相加
System.out.println("两个矩阵相加后的结果:");
showArr(arr);
}
public static void showArr(double[][] arr) {
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++) {
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
public static void getAr
用java编写两个任意大小的矩阵相加
最新推荐文章于 2024-05-02 13:47:31 发布