java矩阵问题 键盘输入两个矩阵然后输出,为什么输出不成了,求大神指点

2025-01-06 05:02:01
推荐回答(1个)
回答1:

import java.util.Scanner;

public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter the number of transition in matrix input: \n");
int transitionA = input.nextInt();
System.out.print("Enter the number of place in matrix input:\n");
int placeA = input.nextInt();
System.out.print("Enter the number of transition in matrix output: \n");
int transitionB = input.nextInt();
System.out.print("Enter the number of place in matrix putput:\n");
int placeB = input.nextInt();
if (transitionA != placeB) {
System.out
.print("To multiply matrix input by matrix output, the number of "
+ "transition in a must be the same as the number"
+ "of place in b.");
System.exit(0);
}
int[][] a = new int[transitionA][placeA];
for (int i1 = 0; i1 < a.length; i1++) {
for (int j1 = 0; j1 < a[0].length; j1++) {
System.out.print("Enter the element a[" + i1 + "][" + j1
+ "]:\n");
a[i1][j1] = input.nextInt();
}

}
int[][] b = new int[transitionB][placeB];
for (int i2 = 0; i2 < b.length; i2++) {
for (int j2 = 0; j2 < b[0].length; j2++) {
System.out.print("Enter the element b[" + i2 + "][" + j2
+ "]:\n");
b[i2][j2] = input.nextInt();
}
}
print(a,b);
}

public static void print(int[][] a, int[][] b) {
System.out.println("the input matrix you enter is:\n");
for (int i1 = 0; i1 < a.length; i1++) {
for (int j1 = 0; j1 < a[i1].length; j1++) {
System.out.print(a[i1][j1] + " ");
}
System.out.println("the output matrix you enter is:\n");
for (int i2 = 0; i2 < b.length; i2++) {
for (int j2 = 0; j2 < b[i2].length; j2++) {
System.out.print(b[i2][j2] + " ");
}
System.out.println();
}
}

}
}