发布网友 发布时间:2024-10-23 23:43
共4个回答
热心网友 时间:2024-11-05 22:18
定义两3*3数组,分别输出矩阵之和跟相乘的结果,代码如下:
public class Problem {
public static void main(String[] args){
int arrayA[][]={{1,2,3},
{4,5,6},
{7,8,9}};
int arrayB[][]={{1,2,3},
{4,5,6},
{7,8,9}};
int arrayC[][] = new int[3][3];
int arrayD[][] = new int[3][3];
int i,j,k;
for (i = 0;i <3;i++){
for (j = 0; j < 3; j++){
arrayC[i][j] = arrayA[i][j]+arrayB[i][j];
for (k = 0; k <3; k++){
arrayD[i][j] += arrayA[i][k]*arrayB[k][j];
}
System.out.print(arrayC[i][j]);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
for (i = 0;i <3;i++){
for (j = 0; j < 3; j++){
System.out.print(arrayD[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
}
热心网友 时间:2024-11-05 22:17
public class Test {
public static void main(String[] args) {
int[][] a = {
{ 1, 1 },{ 1, 1 },{ 1, 1 },
{ 1, 1 },{ 1, 1 },{ 1, 1 },
{ 1, 1 },{ 1, 1 },{ 1, 1 }
};
int addResult = 0;
long mulResult=1;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
addResult += a[i][j];
mulResult*=a[i][j];
}
}
System.out.println(addResult);
System.out.println(mulResult);
}
}
同样遍历第二个数组,将结果求和的相加,求积得相乘。
热心网友 时间:2024-11-05 22:21
public class TestMatrix{
public static void main(String[] args){
int[][] a={{1,2,3,4},{5,6,7,8},{9,1,2,3}};
int[][] b={{1,2},{3,4},{5,6},{7,8}};
int arow=a.length;
int bcol=b[0].length;
int m=b.length;
int[][] c=new int[arow][bcol];
for(int i=0;i<arow;i++){
for(int j=0;j<bcol;j++){
int result=0;
for(int k=0;k<m;k++){
result+=a[i][k]*b[k][j];
}
c[i][j]=result;
}
}
for(int i=0;i<c.length;i++){
for(int j=0;j<c[i].length;j++){
System.out.print(c[i][j]+"\t");
}
System.out.println();
}
}
}
热心网友 时间:2024-11-05 22:23
你好,这样试验下
public class TestMatrix{
public static void main(String[] args){
int[][] a={{1,2,3,4},{5,6,7,8},{9,1,2,3}};
int[][] b={{1,2},{3,4},{5,6},{7,8}};
int arow=a.length;
int bcol=b[0].length;
int m=b.length;
int[][] c=new int[arow][bcol];
for(int i=0;i<arow;i++){
for(int j=0;j<bcol;j++){
int result=0;
for(int k=0;k<m;k++){
result+=a[i][k]*b[k][j];
}
c[i][j]=result;
}
}
for(int i=0;i<c.length;i++){
for(int j=0;j<c[i].length;j++){
System.out.print(c[i][j]+"\t");
}
System.out.println();
}
}
}