为什么矩阵加法在这段代码中无法正常工作?

huangapple go评论71阅读模式
英文:

Why is matrix addition not working in this code?

问题

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();
    }
}
英文:

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(&quot; enter the dimension of the matrix&quot;);
		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(&quot;Enter the elements of the matrix&quot;);
		for (int i = 0; i &lt; n1; i++) {
			for (int j = 0; j &lt; n2; j++) {
				matrix1[i][j] = sc.nextInt();
			 	matrix2[i][j] = matrix1[i][j];
			}
	 	}
	 	// print the arrays
		System.out.println(&quot;matrix1&quot;+Arrays.deepToString(matrix1));
		System.out.println(&quot;matrix2&quot;+Arrays.deepToString(matrix2));
	}

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

答案1

得分: 0

不需要在Matrix()方法内部重新声明实例变量n1n2matrix1matrix2,如果你打算在其他方法中使用它们:

// 获取矩阵的维度
void Matrix() {
    System.out.println("请输入矩阵的维度");
    this.n1 = sc.nextInt();
    this.n2 = sc.nextInt();
    this.matrix1 = new int[n1][n2];
    this.matrix2 = new int[n1][n2]; 
    
    // 获取元素的值
    System.out.println("请输入矩阵的元素");
    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];
        }
    }
    // 打印数组
    System.out.println("matrix1" + Arrays.deepToString(matrix1));
    System.out.println("matrix2" + Arrays.deepToString(matrix2));
}
英文:

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:

// get the dimensions of the matrix//
    void Matrix() {
        System.out.println(&quot; enter the dimension of the matrix&quot;);
        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(&quot;Enter the elements of the matrix&quot;);
        for (int i = 0; i &lt; n1; i++) {
            for (int j = 0; j &lt; n2; j++) {
                matrix1[i][j] = sc.nextInt();
                matrix2[i][j] = matrix1[i][j];
            }
        }
        // print the arrays
        System.out.println(&quot;matrix1&quot;+Arrays.deepToString(matrix1));
        System.out.println(&quot;matrix2&quot;+Arrays.deepToString(matrix2));
    }

答案2

得分: 0

你已经在类的开头声明了 n1n2matrix1matrix2,所以在你的方法 Matrix() 内部不应该重新声明它们,因为它们会被视为局部变量。

你的代码应该像这样:

void Matrix() {
    n1 = 0;
    n2 = 0;
    System.out.println("输入矩阵的维数");
    n1 = sc.nextInt();
    n2 = sc.nextInt();
    matrix1 = new int[n1][n2];
    matrix2 = new int[n1][n2];
    ...
}
英文:

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.

Your code should be something like This :

void Matrix() {
n1 = 0;
n2 = 0;
System.out.println(&quot; enter the dimension of the matrix&quot;);
n1=sc.nextInt();
n2=sc.nextInt();
matrix1= new int [n1][n2];
matrix2= new int [n1][n2];
...  

答案3

得分: -1

Scanner sc = new Scanner(System.in);
System.out.println("输入行数:");
int r = sc.nextInt();
System.out.println("输入列数:");
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("第一个矩阵:");
for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
        arr1[i][j] = sc.nextInt();
    }
}
System.out.println("第二个矩阵:");
for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
        arr2[i][j] = sc.nextInt();
    }
}
System.out.println("第三个矩阵:");
for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
        arr3[i][j] = arr1[i][j] + arr2[i][j];
    }
}
System.out.println("相加结果:");
for (i = 0; i < r; i++) {
    for (j = 0; j < c; j++) {
        System.out.print(arr3[i][j] + " ");
    }
    System.out.println();
}
System.out.println("\n");
英文:
	Scanner sc=new Scanner(System.in);
System.out.println(&quot;enter the size of row:-&quot;);
int r=sc.nextInt();
System.out.println(&quot;enter the column of array:-&quot;);
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(&quot;first matrix:- &quot;);
for(i=0;i&lt;r;i++)
{
for(j=0;j&lt;c;j++)
{
arr1[i][j]=sc.nextInt();
}
}System.out.println(&quot;second matrix:&quot;);
for(i=0;i&lt;r;i++)
{
for(j=0;j&lt;c;j++)
{
arr2[i][j]=sc.nextInt();
}
}System.out.println(&quot;third matrix:- &quot;);
for(i=0;i&lt;r;i++)
{
for(j=0;j&lt;c;j++)
{
arr3[i][j]=arr1[i][j]+arr2[i][j];
}
}System.out.println(&quot;adding is:- &quot;);
for(i=0;i&lt;r;i++)
{
for(j=0;j&lt;c;j++)
{
System.out.println(arr3[i][j]+&quot; &quot;);
}
}System.out.println(&quot;\n&quot;);
}

}

huangapple
  • 本文由 发表于 2020年9月26日 00:21:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64067892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定