不能将int转换为int[],并且对于int[]和int[]类型,运算符未定义。

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

cannot convert int to int[] and operator is undefined for types int[], int []

问题

import java.util.*;

public class Matrix {
	
	public static void main(String[] args) {
		System.out.println("请输入行数:");
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		System.out.println("请输入列数:");
		int column = sc.nextInt();
		
		int[][] a1 = new int[row][column];
		int[][] a2 = new int[row][column];
		
		for (int r = 0; r < row; r++) {
			for (int c = 0; c < column; c++) {
				System.out.println(String.format("请输入第一个 [%d][%d] 整数", r, c));
				a1[r][c] = sc.nextInt(); // 无法将 int 转换为 int[]
			}
		}
		
		for (int r = 0; r < row; r++) {
			for (int c = 0; c < column; c++) {
				System.out.println(String.format("请输入第二个 [%d][%d] 整数", r, c));
				a2[r][c] = sc.nextInt(); // 无法将 int 转换为 int[]
			}
		}

		System.out.println("第一个矩阵:");
		PrintArray(a1);
		System.out.println("第二个矩阵:");
		PrintArray(a2);
		
		add(a1, a2);
		multiply(a1, a2);
	}
	
	public static void add(int[][] a1, int[][] a2) {
		int row = a1.length;
		int column = a1[0].length;
		int[][] sum = new int[row][column];
		
		for (int r = 0; r < row; r++) {
			for (int c = 0; c < column; c++) {
				sum[r][c] = a1[r][c] + a2[r][c]; // "+" 运算符对 int[] 和 int[] 类型未定义
			}
		}
		System.out.println("相加后的矩阵:");
		PrintArray(sum);
	}
	
	public static void multiply(int[][] a1, int[][] a2) {
		int row = a1.length;
		int column = a1[0].length;
		int[][] product = new int[row][column];
		
		for (int r = 0; r < row; r++) {
			for (int c = 0; c < column; c++) {
				product[r][c] = a1[r][c] * a2[r][c]; // "*" 运算符对 int[] 和 int[] 类型未定义
			}
		}
		System.out.println("相乘后的矩阵:");
		PrintArray(product);
	}
	
	public static void PrintArray(int[][] matrix) {
		for (int r = 0; r < matrix.length; r++) {
			for (int c = 0; c < matrix[0].length; c++) {
				System.out.print(matrix[r][c] + "\t");
			}
			System.out.println();
		}
	}
}
英文:

I am having issues printing out my array as my loops will not print due to it saying cannot convert int to int[]. and my addition and multiplication wont concatenate as it says the operator "+" and "*" are undefined for types int[],int[]

If someone could help me figure out why these issues are happening as I though I had it written correctly. IM not very knowledgeable with arrays so any information would be greatly appreciated

import java.util.*;

public class Matrix {
	
	public static void main (String[] args) {
		System.out.println(&quot;Please enter number of rows&quot;);
		Scanner sc = new Scanner(System.in);
		int row = sc.nextInt();
		System.out.println(&quot;Please enter number of columns&quot;);
		int column = sc.nextInt();
		
		int[][] a1 = new int[row][column];
		int[][] a2 = new int[row][column];
		
		for(int r = 0; r &lt; row; r++) {
			for(int c = 0; c &lt; column; c++) {
				System.out.println(String.format(&quot;Enter first [%d][%d] integer&quot;,r,c));


				a1[r]= sc.nextInt();	//cannot convert int to int[]


			}
		}
		
		for(int r = 0; r &lt; row; r++) {
			for(int c = 0; c &lt; column; c++) {
				System.out.println(String.format(&quot;Enter first [%d][%d] integer&quot;,r,c));


				a2[r]= sc.nextInt();	cannot convert int to int[]


			}
		}

		System.out.println(&quot;First Matrix: &quot;);
		PrintArray(a1);
		System.out.println(&quot;First Matrix: &quot;);
		PrintArray(a2);
		
		add(a1,a2);
		multiply(a1,a2);
		
		
		}
	
	public static void add(int[][] a1,int[][] a2) {
		int row = a1.length;
		int column = a1[0].length;
		int[][] add = new int [row][column];
		
		for(int r = 0; r &lt; row; r++) {
			for(int c = 0; c &lt; column; c++) {


				add[r] = (a1[r] + a2[r]); // operator &quot;+&quot; is undefined for types int[],int[]


			}
		}
		System.out.println(&quot;Sum of added arrays: &quot;+ add);
	}
	
	public static void multiply(int[][] a1,int[][] a2) {
		int row = a1.length;
		int column = a1[0].length;
		int[][] add = new int [row][column];
		
		for(int r = 0; r &lt; row; r++) {
			for(int c = 0; c &lt; column; c++) {


				add[r] = (a1[r] * a2[r]); // operator &quot;*&quot; is undefined for types int[],int[]


			}
		}
		System.out.println(&quot;Sum of added arrays: &quot;+ add);
	}
	
	public static void PrintArray(int[][] matrix) {
		for(int r = 0; r &lt; matrix.length; r++ ) {
			for(int c = 0; c &lt;matrix[0].length; c++) {
				System.out.println(matrix[r] + &quot;\t&quot;	);
			}
			System.out.println();
		}
	}
}

答案1

得分: 2

在你的示例中,a1a2 是二维数组。

你的 for 循环迭代是正确的,但是 a1[r] 是一个 int[] 而不是 int

你试图将一个整数值赋值给一个数组,因此出现了“无法转换”的错误。

a1[r] = sc.nextInt();

应更改为:

a1[r][c] = sc.nextInt();

类似地,加法和乘法只适用于基本类型或包装类,而不适用于数组。

add[r] = (a1[r] + a2[r]);

必须更改为:

add[r][c] = (a1[r][c] + a2[r][c]);

以及

add[r] = (a1[r] * a2[r]);

必须更改为:

add[r][c] = (a1[r][c] * a2[r][c]);
英文:

In your example, a1 and a2 are 2 dimensional arrays.

Your for loop iteration is fine, but a1[r] is an int[] and not int.

You are trying to assign an integer value to an array, so you are getting a cannot convert error.

a1[r]= sc.nextInt();

Should be changed to:

a1[r][c] = sc.nextInt(); 

Similarly addition and multiplication are only available for the primitive type or wrappers but not on arrays.

add[r] = (a1[r] + a2[r]);

Has to be changed to:

add[r][c] = (a1[r][c] + a2[r][c]);

And

add[r] = (a1[r] * a2[r]);

Has to be changed to:

add[r][c] = (a1[r][c] * a2[r][c]);

答案2

得分: 2

你需要将这段代码放入程序中,然后它就会正常工作。

你的代码没问题,只是你忘记在一个具有两个维度的数组中使用 a[r][c]。

import java.util.*;

public class Matrix {

    public static void main(String[] args) {
        System.out.println("请输入行数");
        Scanner sc = new Scanner(System.in);
        int row = sc.nextInt();
        System.out.println("请输入列数");
        int column = sc.nextInt();

        int[][] a1 = new int[row][column];
        int[][] a2 = new int[row][column];

        for (int r = 0; r < row; r++) {
            for (int c = 0; c < column; c++) {
                System.out.println(String.format("请输入第一个 [%d][%d] 整数", r, c));
                a1[r][c] = sc.nextInt(); // 2维数组 a1[r] 不正确
            }
        }

        for (int r = 0; r < row; r++) {
            for (int c = 0; c < column; c++) {
                System.out.println(String.format("请输入第二个 [%d][%d] 整数", r, c));
                a2[r][c] = sc.nextInt(); // 2维数组 a2[r] 不正确
            }
        }

        System.out.println("第一个矩阵:");
        PrintArray(a1);
        System.out.println("第二个矩阵:");
        PrintArray(a2);

        add(a1, a2);
        multiply(a1, a2);

    }

    public static void add(int[][] a1, int[][] a2) {
        int row = a1.length;
        int column = a1[0].length;
        int[][] sum = new int[row][column];

        for (int r = 0; r < row; r++) {
            for (int c = 0; c < column; c++) {
                sum[r][c] = a1[r][c] + a2[r][c]; // 操作符"+" 在类型 int[], int[] 上未定义
            }
        }
        System.out.println("两个数组的和:");
        PrintArray(sum);
    }

    public static void multiply(int[][] a1, int[][] a2) {
        int row = a1.length;
        int column = a1[0].length;
        int[][] product = new int[row][column];

        for (int r = 0; r < row; r++) {
            for (int c = 0; c < column; c++) {
                product[r][c] = a1[r][c] * a2[r][c]; // 操作符"*" 在类型 int[], int[] 上未定义
            }
        }
        System.out.println("两个数组的乘积:");
        PrintArray(product);
    }

    public static void PrintArray(int[][] matrix) {
        for (int r = 0; r < matrix.length; r++) {
            for (int c = 0; c < matrix[0].length; c++) {
                System.out.print(matrix[r][c] + "\t");
            }
            System.out.println();
        }
    }
}

例如,对于 2 行 2 列的输入,你的输出将会是:

请输入行数
2
请输入列数
2
请输入第一个 [0][0] 整数
12
请输入第一个 [0][1] 整数
36
请输入第一个 [1][0] 整数
25
请输入第一个 [1][1] 整数
23
请输入第二个 [0][0] 整数
10
请输入第二个 [0][1] 整数
5
请输入第二个 [1][0] 整数
8
请输入第二个 [1][1] 整数
12
第一个矩阵:
12	36	
25	23	
第二个矩阵:
10	5	
8	12	
两个数组的和:
22	41	
33	35	
两个数组的乘积:
120	180	
200	276	
英文:

you have to put this code then it works

your code is fine you just forgot to put an array with two dimensions a[r][c]

import java.util.*;
public class Matrix {
public static void main (String[] args) {
System.out.println(&quot;Please enter number of rows&quot;);
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
System.out.println(&quot;Please enter number of columns&quot;);
int column = sc.nextInt();
int[][] a1 = new int[row][column];
int[][] a2 = new int[row][column];
for(int r = 0; r &lt; row; r++) {
for(int c = 0; c &lt; column; c++) {
System.out.println(String.format(&quot;Enter first [%d][%d] integer&quot;,r,c));
a1[r][c]= sc.nextInt();    // 2 dimensions a1[r] is not fine
}
}
for(int r = 0; r &lt; row; r++) {
for(int c = 0; c &lt; column; c++) {
System.out.println(String.format(&quot;Enter first [%d][%d] integer&quot;,r,c));
a2[r][c]= sc.nextInt();    // 2 dimensions a2[r] is not fine
}
}
System.out.println(&quot;First Matrix: &quot;);
PrintArray(a1);
System.out.println(&quot;First Matrix: &quot;);
PrintArray(a2);
add(a1,a2);
multiply(a1,a2);
}
public static void add(int[][] a1,int[][] a2) {
int row = a1.length;
int column = a1[0].length;
int[][] add = new int [row][column];
for(int r = 0; r &lt; row; r++) {
for(int c = 0; c &lt; column; c++) {
add[r][c] = (a1[r][c] + a2[r][c]); // operator &quot;+&quot; is undefined for types int[],int[]
}
}
System.out.println(&quot;Sum of added arrays: &quot;+ add);
}
public static void multiply(int[][] a1,int[][] a2) {
int row = a1.length;
int column = a1[0].length;
int[][] add = new int [row][column];
for(int r = 0; r &lt; row; r++) {
for(int c = 0; c &lt; column; c++) {
add[r][c] = (a1[r][c] * a2[r][c]); // operator &quot;*&quot; is undefined for types int[],int[]
}
}
System.out.println(&quot;Sum of added arrays: &quot;+ add);
}
public static void PrintArray(int[][] matrix) {
for(int r = 0; r &lt; matrix.length; r++ ) {
for(int c = 0; c &lt;matrix[0].length; c++) {
System.out.println(matrix[r] + &quot;\t&quot; );
}
System.out.println();
}
}
}

your output for 2 row 2 column for example

Please enter number of rows
2
Please enter number of columns
2
Enter first [0][0] integer
12
Enter first [0][1] integer
36
Enter first [1][0] integer
25
Enter first [1][1] integer
23

huangapple
  • 本文由 发表于 2020年10月9日 05:12:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64270682.html
匿名

发表评论

匿名网友

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

确定