我正在尝试编译这个程序,我输入了两个3 * 3阵列,我正在进行多次拼接并添加它们。但是它最终给了我一些错误,因为它没有运行。我无法删除这些错误,我需要有关如何删除它们的帮助。
import java.util.Scanner;
public class Matrix{
public static void main(String [] args){
int a [][]= new int [3][3];
int b [][]= new int [3][3];
int C [][]= new int [3][3];
int d [][]= new int [3][3];
Scanner in= new Scanner(System.in);
System.out.println("Enter Numbers in first matrix");
for( int r=0; r<3; r++){
for( int c=0; c<3; c++){
a [r][c]= in.nextInt();
}
}
System.out.println("Matrix 1 : ");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
System.out.print(" "+ a[i][j]);
}
System.out.println();
}
System.out.println("Enter Numbers in 2nd matrix");
for( int n=0; n<3; n++){
for( int m=0; m<3; m++){
b [n][m]= in.nextInt();
}
}
System.out.println("Matrix 2 : ");
for(int k = 0; k < 3; k++) {
for(int l = 0; l < 3; l++) {
System.out.print(" "+b[k][l]);
}
System.out.println();
}
//for multiplication of the matrices
for(int f = 0; f < 3; f++) {
for(int s = 0; s < 3; s++) {
for(int d = 0; d < 3; d++){
C[f][s] += a[f][d]*b[d][s];
}
}
}
System.out.println("Multiplication of both matrix : ");
for(int x = 0; x < 3; x++) {
for(int y = 0; y < 3; y++) {
System.out.print(" "+C[x][y]);
}
System.out.println();
}
}
// for addition of both matrices
for(int w=0; w < 3; w++) {
for(int u=0; u < 3; u++) {
d[w][u] = a[w][u]+b[w][u];
}
}
System.out.println("Addition of both matrix : ");
for(int p = 0; p < 3; p++) {
for(int q = 0; q < 3; q++) {
System.out.print(" "+ d[p][q]);
}
System.out.println();
}
}
}
答案 0 :(得分:1)
使用IDE并缩进程序。你会看到问题。您有两个问题:
同样,是时候下载并使用IDE和缩进代码了。
答案 1 :(得分:0)
如果您正确识别代码,您会发现自己有额外的&#34;}&#34;:
System.out.println("Multiplication of both matrix : ");
for(int x = 0; x < 3; x++) {
for(int y = 0; y < 3; y++) {
System.out.print(" "+C[x][y]);
}
System.out.println();
}
} // Remove this
您还有两次变量d
:
int d [][]= new int [3][3];
...
for(int d = 0; d < 3; d++){