两个矩阵相加 Exercise08_05

时间:2021-10-10 11:02:49
 import java.util.Scanner;
/**
* @author 冰樱梦
* 时间:2018年12月
* 题目:两个矩阵相加
*
*/
public class Exercise08_05 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
double matrix1[][]=new double[3][3];
double matrix2[][]=new double[3][3]; //给数组1赋值
System.out.println("Enter matrix1: ");
for(int i=0;i<matrix1.length;i++){
for(int j=0;j<matrix1[i].length;j++){
matrix1[i][j]=input.nextDouble();
}
} //给数组2赋值
System.out.println("Enter matrix2: ");
for(int i=0;i<matrix2.length;i++){
for(int j=0;j<matrix2[i].length;j++){
matrix2[i][j]=input.nextDouble();
}
} System.out.println("The matrices are added as follows"); //输出数组1
for(int i=0;i<matrix1.length;i++){
for(int j=0;j<matrix1[i].length;j++){
System.out.print(matrix1[i][j]+"\t");
}
System.out.println();
} System.out.println("+"); //输出数组2
for(int i=0;i<matrix2.length;i++){
for(int j=0;j<matrix2[i].length;j++){
System.out.print(matrix2[i][j]+"\t");
}
System.out.println();
} System.out.println("="); //输出结果
for(int i=0;i<matrix2.length;i++){
for(int j=0;j<matrix2[i].length;j++){
System.out.print(matrix1[i][j]+matrix2[i][j]+"\t");
}
System.out.println();
}
}
}