两个矩阵相加 Exercise08_05

 1 import java.util.Scanner;
 2 /**
 3  * @author 冰樱梦
 4  * 时间:2018年12月
 5  * 题目:两个矩阵相加
 6  *
 7  */
 8 public class Exercise08_05 {
 9     public static void main(String[] args){
10         Scanner input=new Scanner(System.in);
11         double matrix1[][]=new double[3][3];
12         double matrix2[][]=new double[3][3];
13         
14         //给数组1赋值
15         System.out.println("Enter matrix1: ");
16         for(int i=0;i<matrix1.length;i++){
17             for(int j=0;j<matrix1[i].length;j++){
18                 matrix1[i][j]=input.nextDouble();
19             }
20         }
21         
22         //给数组2赋值
23         System.out.println("Enter matrix2: ");
24         for(int i=0;i<matrix2.length;i++){
25             for(int j=0;j<matrix2[i].length;j++){
26                 matrix2[i][j]=input.nextDouble();
27             }
28         }
29         
30         System.out.println("The matrices are added as follows");
31         
32         //输出数组1
33         for(int i=0;i<matrix1.length;i++){
34             for(int j=0;j<matrix1[i].length;j++){
35                 System.out.print(matrix1[i][j]+"\t");
36             }
37             System.out.println();
38         }
39         
40         System.out.println("+");
41         
42         //输出数组2
43         for(int i=0;i<matrix2.length;i++){
44             for(int j=0;j<matrix2[i].length;j++){
45                 System.out.print(matrix2[i][j]+"\t");
46             }
47             System.out.println();
48         }
49         
50         System.out.println("=");
51         
52         //输出结果
53         for(int i=0;i<matrix2.length;i++){
54             for(int j=0;j<matrix2[i].length;j++){
55                 System.out.print(matrix1[i][j]+matrix2[i][j]+"\t");
56             }
57             System.out.println();
58         }
59     }
60 }

 

posted @ 2018-12-25 15:24  CHERRYL  阅读(371)  评论(0编辑  收藏  举报