英文:
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(" 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();
}
}
答案1
得分: 0
不需要在Matrix()
方法内部重新声明实例变量n1
、n2
、matrix1
、matrix2
,如果你打算在其他方法中使用它们:
// 获取矩阵的维度
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(" 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));
}
答案2
得分: 0
你已经在类的开头声明了 n1
、n2
、matrix1
和 matrix2
,所以在你的方法 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(" enter the dimension of the matrix");
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("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");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论