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

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

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

问题

  1. import java.util.*;
  2. public class Matrix {
  3. public static void main(String[] args) {
  4. System.out.println("请输入行数:");
  5. Scanner sc = new Scanner(System.in);
  6. int row = sc.nextInt();
  7. System.out.println("请输入列数:");
  8. int column = sc.nextInt();
  9. int[][] a1 = new int[row][column];
  10. int[][] a2 = new int[row][column];
  11. for (int r = 0; r < row; r++) {
  12. for (int c = 0; c < column; c++) {
  13. System.out.println(String.format("请输入第一个 [%d][%d] 整数", r, c));
  14. a1[r][c] = sc.nextInt(); // 无法将 int 转换为 int[]
  15. }
  16. }
  17. for (int r = 0; r < row; r++) {
  18. for (int c = 0; c < column; c++) {
  19. System.out.println(String.format("请输入第二个 [%d][%d] 整数", r, c));
  20. a2[r][c] = sc.nextInt(); // 无法将 int 转换为 int[]
  21. }
  22. }
  23. System.out.println("第一个矩阵:");
  24. PrintArray(a1);
  25. System.out.println("第二个矩阵:");
  26. PrintArray(a2);
  27. add(a1, a2);
  28. multiply(a1, a2);
  29. }
  30. public static void add(int[][] a1, int[][] a2) {
  31. int row = a1.length;
  32. int column = a1[0].length;
  33. int[][] sum = new int[row][column];
  34. for (int r = 0; r < row; r++) {
  35. for (int c = 0; c < column; c++) {
  36. sum[r][c] = a1[r][c] + a2[r][c]; // "+" 运算符对 int[] 和 int[] 类型未定义
  37. }
  38. }
  39. System.out.println("相加后的矩阵:");
  40. PrintArray(sum);
  41. }
  42. public static void multiply(int[][] a1, int[][] a2) {
  43. int row = a1.length;
  44. int column = a1[0].length;
  45. int[][] product = new int[row][column];
  46. for (int r = 0; r < row; r++) {
  47. for (int c = 0; c < column; c++) {
  48. product[r][c] = a1[r][c] * a2[r][c]; // "*" 运算符对 int[] 和 int[] 类型未定义
  49. }
  50. }
  51. System.out.println("相乘后的矩阵:");
  52. PrintArray(product);
  53. }
  54. public static void PrintArray(int[][] matrix) {
  55. for (int r = 0; r < matrix.length; r++) {
  56. for (int c = 0; c < matrix[0].length; c++) {
  57. System.out.print(matrix[r][c] + "\t");
  58. }
  59. System.out.println();
  60. }
  61. }
  62. }
英文:

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

  1. import java.util.*;
  2. public class Matrix {
  3. public static void main (String[] args) {
  4. System.out.println(&quot;Please enter number of rows&quot;);
  5. Scanner sc = new Scanner(System.in);
  6. int row = sc.nextInt();
  7. System.out.println(&quot;Please enter number of columns&quot;);
  8. int column = sc.nextInt();
  9. int[][] a1 = new int[row][column];
  10. int[][] a2 = new int[row][column];
  11. for(int r = 0; r &lt; row; r++) {
  12. for(int c = 0; c &lt; column; c++) {
  13. System.out.println(String.format(&quot;Enter first [%d][%d] integer&quot;,r,c));
  14. a1[r]= sc.nextInt(); //cannot convert int to int[]
  15. }
  16. }
  17. for(int r = 0; r &lt; row; r++) {
  18. for(int c = 0; c &lt; column; c++) {
  19. System.out.println(String.format(&quot;Enter first [%d][%d] integer&quot;,r,c));
  20. a2[r]= sc.nextInt(); cannot convert int to int[]
  21. }
  22. }
  23. System.out.println(&quot;First Matrix: &quot;);
  24. PrintArray(a1);
  25. System.out.println(&quot;First Matrix: &quot;);
  26. PrintArray(a2);
  27. add(a1,a2);
  28. multiply(a1,a2);
  29. }
  30. public static void add(int[][] a1,int[][] a2) {
  31. int row = a1.length;
  32. int column = a1[0].length;
  33. int[][] add = new int [row][column];
  34. for(int r = 0; r &lt; row; r++) {
  35. for(int c = 0; c &lt; column; c++) {
  36. add[r] = (a1[r] + a2[r]); // operator &quot;+&quot; is undefined for types int[],int[]
  37. }
  38. }
  39. System.out.println(&quot;Sum of added arrays: &quot;+ add);
  40. }
  41. public static void multiply(int[][] a1,int[][] a2) {
  42. int row = a1.length;
  43. int column = a1[0].length;
  44. int[][] add = new int [row][column];
  45. for(int r = 0; r &lt; row; r++) {
  46. for(int c = 0; c &lt; column; c++) {
  47. add[r] = (a1[r] * a2[r]); // operator &quot;*&quot; is undefined for types int[],int[]
  48. }
  49. }
  50. System.out.println(&quot;Sum of added arrays: &quot;+ add);
  51. }
  52. public static void PrintArray(int[][] matrix) {
  53. for(int r = 0; r &lt; matrix.length; r++ ) {
  54. for(int c = 0; c &lt;matrix[0].length; c++) {
  55. System.out.println(matrix[r] + &quot;\t&quot; );
  56. }
  57. System.out.println();
  58. }
  59. }
  60. }

答案1

得分: 2

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

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

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

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

应更改为:

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

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

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

必须更改为:

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

以及

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

必须更改为:

  1. 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.

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

Should be changed to:

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

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

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

Has to be changed to:

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

And

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

Has to be changed to:

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

答案2

得分: 2

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

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

  1. import java.util.*;
  2. public class Matrix {
  3. public static void main(String[] args) {
  4. System.out.println("请输入行数");
  5. Scanner sc = new Scanner(System.in);
  6. int row = sc.nextInt();
  7. System.out.println("请输入列数");
  8. int column = sc.nextInt();
  9. int[][] a1 = new int[row][column];
  10. int[][] a2 = new int[row][column];
  11. for (int r = 0; r < row; r++) {
  12. for (int c = 0; c < column; c++) {
  13. System.out.println(String.format("请输入第一个 [%d][%d] 整数", r, c));
  14. a1[r][c] = sc.nextInt(); // 2维数组 a1[r] 不正确
  15. }
  16. }
  17. for (int r = 0; r < row; r++) {
  18. for (int c = 0; c < column; c++) {
  19. System.out.println(String.format("请输入第二个 [%d][%d] 整数", r, c));
  20. a2[r][c] = sc.nextInt(); // 2维数组 a2[r] 不正确
  21. }
  22. }
  23. System.out.println("第一个矩阵:");
  24. PrintArray(a1);
  25. System.out.println("第二个矩阵:");
  26. PrintArray(a2);
  27. add(a1, a2);
  28. multiply(a1, a2);
  29. }
  30. public static void add(int[][] a1, int[][] a2) {
  31. int row = a1.length;
  32. int column = a1[0].length;
  33. int[][] sum = new int[row][column];
  34. for (int r = 0; r < row; r++) {
  35. for (int c = 0; c < column; c++) {
  36. sum[r][c] = a1[r][c] + a2[r][c]; // 操作符"+" 在类型 int[], int[] 上未定义
  37. }
  38. }
  39. System.out.println("两个数组的和:");
  40. PrintArray(sum);
  41. }
  42. public static void multiply(int[][] a1, int[][] a2) {
  43. int row = a1.length;
  44. int column = a1[0].length;
  45. int[][] product = new int[row][column];
  46. for (int r = 0; r < row; r++) {
  47. for (int c = 0; c < column; c++) {
  48. product[r][c] = a1[r][c] * a2[r][c]; // 操作符"*" 在类型 int[], int[] 上未定义
  49. }
  50. }
  51. System.out.println("两个数组的乘积:");
  52. PrintArray(product);
  53. }
  54. public static void PrintArray(int[][] matrix) {
  55. for (int r = 0; r < matrix.length; r++) {
  56. for (int c = 0; c < matrix[0].length; c++) {
  57. System.out.print(matrix[r][c] + "\t");
  58. }
  59. System.out.println();
  60. }
  61. }
  62. }

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

  1. 请输入行数
  2. 2
  3. 请输入列数
  4. 2
  5. 请输入第一个 [0][0] 整数
  6. 12
  7. 请输入第一个 [0][1] 整数
  8. 36
  9. 请输入第一个 [1][0] 整数
  10. 25
  11. 请输入第一个 [1][1] 整数
  12. 23
  13. 请输入第二个 [0][0] 整数
  14. 10
  15. 请输入第二个 [0][1] 整数
  16. 5
  17. 请输入第二个 [1][0] 整数
  18. 8
  19. 请输入第二个 [1][1] 整数
  20. 12
  21. 第一个矩阵:
  22. 12 36
  23. 25 23
  24. 第二个矩阵:
  25. 10 5
  26. 8 12
  27. 两个数组的和:
  28. 22 41
  29. 33 35
  30. 两个数组的乘积:
  31. 120 180
  32. 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]

  1. import java.util.*;
  2. public class Matrix {
  3. public static void main (String[] args) {
  4. System.out.println(&quot;Please enter number of rows&quot;);
  5. Scanner sc = new Scanner(System.in);
  6. int row = sc.nextInt();
  7. System.out.println(&quot;Please enter number of columns&quot;);
  8. int column = sc.nextInt();
  9. int[][] a1 = new int[row][column];
  10. int[][] a2 = new int[row][column];
  11. for(int r = 0; r &lt; row; r++) {
  12. for(int c = 0; c &lt; column; c++) {
  13. System.out.println(String.format(&quot;Enter first [%d][%d] integer&quot;,r,c));
  14. a1[r][c]= sc.nextInt(); // 2 dimensions a1[r] is not fine
  15. }
  16. }
  17. for(int r = 0; r &lt; row; r++) {
  18. for(int c = 0; c &lt; column; c++) {
  19. System.out.println(String.format(&quot;Enter first [%d][%d] integer&quot;,r,c));
  20. a2[r][c]= sc.nextInt(); // 2 dimensions a2[r] is not fine
  21. }
  22. }
  23. System.out.println(&quot;First Matrix: &quot;);
  24. PrintArray(a1);
  25. System.out.println(&quot;First Matrix: &quot;);
  26. PrintArray(a2);
  27. add(a1,a2);
  28. multiply(a1,a2);
  29. }
  30. public static void add(int[][] a1,int[][] a2) {
  31. int row = a1.length;
  32. int column = a1[0].length;
  33. int[][] add = new int [row][column];
  34. for(int r = 0; r &lt; row; r++) {
  35. for(int c = 0; c &lt; column; c++) {
  36. add[r][c] = (a1[r][c] + a2[r][c]); // operator &quot;+&quot; is undefined for types int[],int[]
  37. }
  38. }
  39. System.out.println(&quot;Sum of added arrays: &quot;+ add);
  40. }
  41. public static void multiply(int[][] a1,int[][] a2) {
  42. int row = a1.length;
  43. int column = a1[0].length;
  44. int[][] add = new int [row][column];
  45. for(int r = 0; r &lt; row; r++) {
  46. for(int c = 0; c &lt; column; c++) {
  47. add[r][c] = (a1[r][c] * a2[r][c]); // operator &quot;*&quot; is undefined for types int[],int[]
  48. }
  49. }
  50. System.out.println(&quot;Sum of added arrays: &quot;+ add);
  51. }
  52. public static void PrintArray(int[][] matrix) {
  53. for(int r = 0; r &lt; matrix.length; r++ ) {
  54. for(int c = 0; c &lt;matrix[0].length; c++) {
  55. System.out.println(matrix[r] + &quot;\t&quot; );
  56. }
  57. System.out.println();
  58. }
  59. }
  60. }

your output for 2 row 2 column for example

  1. Please enter number of rows
  2. 2
  3. Please enter number of columns
  4. 2
  5. Enter first [0][0] integer
  6. 12
  7. Enter first [0][1] integer
  8. 36
  9. Enter first [1][0] integer
  10. 25
  11. Enter first [1][1] integer
  12. 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:

确定