简体   繁体   English

为什么矩阵加法在这段代码中不起作用?

[英]Why is matrix addition not working in this code?

Can anyone tell me why matrix addition is not working in this code?谁能告诉我为什么矩阵加法在这段代码中不起作用? All other parts of the program are working except the addition part.除了加法部分,程序的所有其他部分都在工作。 It's just printing an empty array as output.它只是打印一个空数组作为输出。

import java.util.*;

class matrix{
    int n1, n2 = 0;
    int[][] matrix1 = new int [n1][n2];
    int[][] matrix2 = new int [n1][n2];
    int[][] matrix3 = new int [n1][n2];
    Scanner sc = new Scanner(System.in);
     
    // get the dimensions of the matrix//
    void Matrix() {
        int n1, n2 = 0;
        System.out.println(" enter the dimension of the matrix");
        n1 = sc.nextInt();
        n2 = sc.nextInt();
        int[][] matrix1 = new int [n1][n2];
        int[][] matrix2 = new int [n1][n2]; 
    
     
        //get the values of the elements//
        System.out.println("Enter the elements of the matrix");
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                matrix1[i][j] = sc.nextInt();
                matrix2[i][j] = matrix1[i][j];
            }
        }
        // print the arrays
        System.out.println("matrix1"+Arrays.deepToString(matrix1));
        System.out.println("matrix2"+Arrays.deepToString(matrix2));
    }

    void add() {
        int[][] matrix3 = new int[n1][n2];
        for (int i = 0; i < n1; i++) {
            for(int j=0; j < n2; j++) {
                matrix3[i][j] = matrix1[i][j] + matrix2[i][j];
            }
        }
        System.out.println("Addition of the elements" + Arrays.deepToString(matrix3));
    }
}
public class arrays2 {
    public static void main(String[] args) {
        matrix M1 = new matrix();
        M1.Matrix();
        M1.add();   
    }
}

You do not need to redeclare instance variables n1 , n2 , matrix1 , matrix2 inside method Matrix() if you are going to use them in other method:如果您打算在其他方法中使用它们,则无需在方法Matrix()重新声明实例变量n1n2matrix1matrix2

// get the dimensions of the matrix//
    void Matrix() {
        System.out.println(" enter the dimension of the matrix");
        this.n1 = sc.nextInt();
        this.n2 = sc.nextInt();
        this.matrix1 = new int [n1][n2];
        this.matrix2 = new int [n1][n2]; 
     
        //get the values of the elements//
        System.out.println("Enter the elements of the matrix");
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                matrix1[i][j] = sc.nextInt();
                matrix2[i][j] = matrix1[i][j];
            }
        }
        // print the arrays
        System.out.println("matrix1"+Arrays.deepToString(matrix1));
        System.out.println("matrix2"+Arrays.deepToString(matrix2));
    }

You already declared n1 , n2 , matrix1 and matrix2 at the beginning of your class, so you shouldn't redeclare them inside your method Matrix() because they'll be considered as local variables.您已经在类的开头声明了n1n2matrix1matrix2 ,因此您不应在方法Matrix()重新声明它们,因为它们将被视为局部变量。

Your code should be something like This :你的代码应该是这样的:

void Matrix() {
n1 = 0;
n2 = 0;
System.out.println(" enter the dimension of the matrix");
n1=sc.nextInt();
n2=sc.nextInt();
matrix1= new int [n1][n2];
matrix2= new int [n1][n2];
...  
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the size of row:-");
    int r=sc.nextInt();
    System.out.println("enter the column of array:-");
    int c=sc.nextInt();
    int[][] arr1=new int[r][c];
    int[][] arr2=new int[r][c];
    int[][] arr3=new int[r][c];
    int i,j,k,l,m,n;
    System.out.println("first matrix:- ");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            arr1[i][j]=sc.nextInt();
        }
    }System.out.println("second matrix:");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            arr2[i][j]=sc.nextInt();
        }
    }System.out.println("third matrix:- ");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            arr3[i][j]=arr1[i][j]+arr2[i][j];
        }
    }System.out.println("adding is:- ");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            System.out.println(arr3[i][j]+" ");
        }
    }System.out.println("\n");
    
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM