0

第 10 行出现错误,说明有关 long 的内容。但我不明白为什么会出现这样的错误,因为 int 也可以作为参数。另外,如果是重复的问题,请发送原始问题的链接。

import java.util.Arrays;
import java.util.Scanner;

public class Matrix {

public static void main(String[] args) {
int[][] matrix = Matrix.matrixCreator();
    for(int p = 0; p < matrix.length; p++) {
        for(int o = 0; o < matrix[p].length; o++) {
    System.out.println(Arrays.toString(matrix[p][o]));
 }
    }
}

public static int[][] matrixCreator() {
    int[][] matrix = new int[3][3];
     Scanner scan = new Scanner(System.in);
    int[] convertToInt = new int[3];

    for(int position = 1; position <= matrix.length; position++) {
        System.out.printf("Enter the elements of the matrix in row %d separating them with spaces: " , position);
        convertToInt[0] = scan.nextInt();
        convertToInt[1] = scan.nextInt();
        convertToInt[2] = scan.nextInt();

    matrix[position - 1] = convertToInt;
    }
    return matrix;
}
}

它显示的错误是

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method toString(long[]) in the type Arrays is not applicable for the arguments (ints)

    at Matrix.main(Matrix.java:10)
4

2 回答 2

0

您不能单独使用该单元格,而应使用一个完整的一维数组作为Arrays.toString. 在给定的代码中,您也不需要内部循环。

System.out.println(Arrays.toString(matrix[p]));

如果您使用双循环,则使用 Arrays.toString 只是一种矫枉过正。在这种情况下,您可以简单地使用以下内容。

System.out.println(matrix[p][o]);
于 2019-08-03T03:29:18.410 回答
0

实际上,在“Arrays.toString(...)”中,您需要传递一个数组,而不是一个 int。

例如

System.out.println(Arrays.toString(matrix[p]));

在您的情况下,矩阵是一个二维数组。如果它是三维的,那么这将起作用。

System.out.println(Arrays.toString(matrix[p][o]));

由于“matrix[p][o]”的返回将是一个单维数组。

于 2019-08-03T03:37:13.743 回答