gpt4 book ai didi

java - 尝试打印二维数组(矩阵)时出错

转载 作者:行者123 更新时间:2023-12-02 02:09:47 24 4
gpt4 key购买 nike

第 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)

最佳答案

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

例如

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

在你的例子中,矩阵是一个二维数组。如果它是三维的,那么这将起作用。

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

因为“matrix[p][o]”的返回将是一个一维数组。

关于java - 尝试打印二维数组(矩阵)时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57335104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com