如何编写Java程序以添加两个矩阵
要添加两个矩阵-
创建一个空矩阵
在新矩阵的每个位置,从给定的两个矩阵分配相同位置的值之和,即,如果A[i][j]和B[i][j]是两个给定的矩阵,则c[i][j]应该是A[i][j]+B[i][j]
示例
public class AddingTwoMatrices{
public static void main(String args[]){
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{1,1,1},{1,1,1},{1,1,1}};
int c[][]=new int[3][3];
for(int i = 0;i<3;i++){
for(int j = 0;j<3;j++){
c[i][j] = a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}输出结果
2 3 4 5 6 7 8 9 10
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短