Java - 获取编译错误不兼容的类型int无法转换为[] []

时间:2021-06-08 16:26:42

Having trouble understanding compile error. Main should be left unchanged. I believe MakeandFillMatix method is the problem and I can't figure it out. I think it's how I created the method.

无法理解编译错误。主要应保持不变。我相信MakeandFillMatix方法是问题,我无法弄明白。我认为这就是我创建方法的方式。

import java.util.*;
/*

*/
public class TesterProject
{
    public static void main(String [] args)
    {
        int n = getMatrixSize();
        int[][] m = makeAndFillMatrix(n);
        printMatrix(m);
    }
    public static int getMatrixSize()
    {
        Scanner S = new Scanner(System.in);

        System.out.println("give me a int to create the matrix");
        int n = S.nextint();
        return n;
    }
    public static void makeAndFillMatrix(int [][] r)
    {
        Random generator = new Random(5);
        int rand = generator.nextInt(10);
        for(int i = 0; i < r.length; i++)
        {
            for(int j = 0; j < r; j++)
            {
                r[i][j]= rand;
            }
        }
        return r;
    }
    public static void printMatrix(int [][] matrix)
    {
        for(int r = 0; r < matrix.length; r++)
        {
            for(int c = 0; c < matrix[r].length; c++)
            {
                System.out.print(matrix[r][c] + " ");
            }
            System.out.println();
        }
    }
}

4 个解决方案

#1


2  

Hint

You have many problems in your code :

您的代码中存在许多问题:

  1. int n = S.nextint(); it should be int n = S.nextInt(); with Upper I
  2. int n = S.nextint();它应该是int n = S.nextInt();上层我

  3. You can't compare an int with an array for (int j = 0; j < r; j++) { i think you need this for (int j = 0; j < i; j++) {
  4. 你不能将int与数组进行比较(int j = 0; j ;>

  5. void makeAndFillMatrix(int[][] r) it not return any thing, and your return an array in the end.
  6. void makeAndFillMatrix(int [] [] r)它不返回任何东西,并且你最后返回一个数组。

  7. makeAndFillMatrix(int[][] r) take an array of 2D and not an int int[][] m = makeAndFillMatrix(n);
  8. makeAndFillMatrix(int [] [] r)取一个2D数组而不是int int [] [] m = makeAndFillMatrix(n);

Fix this problem and your problem is solve :)

解决这个问题,你的问题解决了:)


EDIT

Then you have to change your code to be like this :

然后你必须改变你的代码是这样的:

public static void main(String[] args) {
    int n = getMatrixSize();
    int[][] m = makeAndFillMatrix(n);//<<<----Problem 4
    printMatrix(m);
}

public static int getMatrixSize() {
    Scanner S = new Scanner(System.in);

    System.out.println("give me a int to create the matrix");
    int n = S.nextInt();//<<--------------Problem 1
    return n;
}

public static int[][] makeAndFillMatrix(int n) {//<<<---Problem 3
    Random generator = new Random(5);
    int[][] r = new int[n][n];
    int rand = generator.nextInt(10);
    for (int i = 0; i < r.length; i++) {
        for (int j = 0; j < i; j++) {//<<<-----------Problem 2
            r[i][j] = rand;
        }
    }
    return r;
}

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

#2


0  

Change makeAndFillMatrix(int[][] r) to makeAndFillMatrix(int r) You want the matrix size, which is an integer, so take integer instead of a multidimensional array.

将makeAndFillMatrix(int [] [] r)更改为makeAndFillMatrix(int r)您需要矩阵大小,这是一个整数,因此取整数而不是多维数组。

And in the for loop after that, change r.length to just r

在之后的for循环中,将r.length更改为r

Change void to int [][] in the method declaration.

在方法声明中将void更改为int [] []。

In the method add a line int[][] q; and change r[i][j] = rand to q[i][j] = rand

在方法中添加一行int [] [] q;并将r [i] [j] = rand改为q [i] [j] = rand

And change return r to return q

并更改返回r以返回q

#3


0  

The error you are receiving is due to you calling

您收到的错误是由于您打电话

int[][] m = makeAndFillMatrix(n);

in your main method.

在你的主要方法。

You set n as an int in the line above, meaning n is a number, BUT, makeAndFillMatrix(int[][]) takes in a 2d array of ints. (A 2d array is a matrix, by the way.)

你在上面的行中将n设置为int,意味着n是一个数字,但是,makeAndFillMatrix(int [] [])接受一个2d的int数组。 (顺便说一句,2d数组是一个矩阵。)

If you want to make a matrix of the size entered in by the user, then instead of passing n into your makeAndFillMatrix method, you would need to pass in a matrix of size n. You can do this in two ways. Option 1 is to pass in the matrix to your makeAndFillMatrix method like so:

如果要创建用户输入的大小的矩阵,则不需要将n传递到makeAndFillMatrix方法中,而是需要传入大小为n的矩阵。你可以用两种方式做到这一点。选项1是将矩阵传递给makeAndFillMatrix方法,如下所示:

public static void main(String [] args)
{
    int n = getMatrixSize();
    int[][] m = makeAndFillMatrix(new int[n][n]); // new int matrix of size n
    printMatrix(m);
}

Or (the more conventional way, in my opinion), you can give makeAndFillMatrix the size of the matrix (like you are doing), and you can have makeAndFillMatrix make the array:

或者(在我看来,更传统的方式),你可以给makeAndFillMatrix矩阵的大小(就像你正在做的那样),你可以让makeAndFillMatrix创建数组:

public static void makeAndFillMatrix(int matrixSize)
{
    int[][] r = new int[matrixSize][matrixSize];
    Random generator = new Random(5);
    int rand = generator.nextInt(10);
    for(int i = 0; i < r.length; i++)
    {
        for(int j = 0; j < r[i].length; j++) //~~~I modified this line as well~~~
        {
            r[i][j]= rand;
        }
    }
    return r;
}

Anyways, you can take your pick at which approach you want to take to this problem, but regardless, I noticed that your makeAndFillMatrix method had an error. I fixed it and wrote //~~~I modified this line as well~~~

无论如何,你可以选择你想要采用哪种方法解决这个问题,但无论如何,我注意到你的makeAndFillMatrix方法有错误。我修好了它并写了// ~~~我修改了这一行~~~

Your inner for loop originally said for(int j = 0; j < r; j++), but r is a matrix. For the inner loop, you would want to increase j until it has reached the size of the current row you are on in the matrix, so I changed that line as well. (Your inner loop was correct in your printMatrix method.) Whichever approach you take for your first issue, the inner loop in your makeAndFillMatrix method will need to be modified as well.

你的内部for循环最初表示为(int j = 0; j ;>

I hope this helps!

我希望这有帮助!

#4


0  

You are trying to cast 'int' (in the main function) to an 'int[][]' (the parameter of 'makeAndFillMatrix'. Also, makeAndFillMatrix returns 'void'.

您正在尝试将'int'(在main函数中)转换为'int [] []'('makeAndFillMatrix'的参数。此外,makeAndFillMatrix返回'void'。

I believe you want to rewrite your function to something like this:

我相信你想要将你的功能改写成这样的东西:

public static int[][] makeAndFillMatrix(int r)
{
    // Create a new matrix
    int[][] matrix = new int[r][r];
    // Get a random value
    Random generator = new Random(5);
    int rand = generator.nextInt(10);
    // Assign random value to each cell of the matrix
    for(int i = 0; i < r; i++)
    {
        for(int j = 0; j < r; j++)
        {
            r[i][j] = rand;
        }
    }
    // Return the matrix
    return matrix;
}

#1


2  

Hint

You have many problems in your code :

您的代码中存在许多问题:

  1. int n = S.nextint(); it should be int n = S.nextInt(); with Upper I
  2. int n = S.nextint();它应该是int n = S.nextInt();上层我

  3. You can't compare an int with an array for (int j = 0; j < r; j++) { i think you need this for (int j = 0; j < i; j++) {
  4. 你不能将int与数组进行比较(int j = 0; j ;>

  5. void makeAndFillMatrix(int[][] r) it not return any thing, and your return an array in the end.
  6. void makeAndFillMatrix(int [] [] r)它不返回任何东西,并且你最后返回一个数组。

  7. makeAndFillMatrix(int[][] r) take an array of 2D and not an int int[][] m = makeAndFillMatrix(n);
  8. makeAndFillMatrix(int [] [] r)取一个2D数组而不是int int [] [] m = makeAndFillMatrix(n);

Fix this problem and your problem is solve :)

解决这个问题,你的问题解决了:)


EDIT

Then you have to change your code to be like this :

然后你必须改变你的代码是这样的:

public static void main(String[] args) {
    int n = getMatrixSize();
    int[][] m = makeAndFillMatrix(n);//<<<----Problem 4
    printMatrix(m);
}

public static int getMatrixSize() {
    Scanner S = new Scanner(System.in);

    System.out.println("give me a int to create the matrix");
    int n = S.nextInt();//<<--------------Problem 1
    return n;
}

public static int[][] makeAndFillMatrix(int n) {//<<<---Problem 3
    Random generator = new Random(5);
    int[][] r = new int[n][n];
    int rand = generator.nextInt(10);
    for (int i = 0; i < r.length; i++) {
        for (int j = 0; j < i; j++) {//<<<-----------Problem 2
            r[i][j] = rand;
        }
    }
    return r;
}

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

#2


0  

Change makeAndFillMatrix(int[][] r) to makeAndFillMatrix(int r) You want the matrix size, which is an integer, so take integer instead of a multidimensional array.

将makeAndFillMatrix(int [] [] r)更改为makeAndFillMatrix(int r)您需要矩阵大小,这是一个整数,因此取整数而不是多维数组。

And in the for loop after that, change r.length to just r

在之后的for循环中,将r.length更改为r

Change void to int [][] in the method declaration.

在方法声明中将void更改为int [] []。

In the method add a line int[][] q; and change r[i][j] = rand to q[i][j] = rand

在方法中添加一行int [] [] q;并将r [i] [j] = rand改为q [i] [j] = rand

And change return r to return q

并更改返回r以返回q

#3


0  

The error you are receiving is due to you calling

您收到的错误是由于您打电话

int[][] m = makeAndFillMatrix(n);

in your main method.

在你的主要方法。

You set n as an int in the line above, meaning n is a number, BUT, makeAndFillMatrix(int[][]) takes in a 2d array of ints. (A 2d array is a matrix, by the way.)

你在上面的行中将n设置为int,意味着n是一个数字,但是,makeAndFillMatrix(int [] [])接受一个2d的int数组。 (顺便说一句,2d数组是一个矩阵。)

If you want to make a matrix of the size entered in by the user, then instead of passing n into your makeAndFillMatrix method, you would need to pass in a matrix of size n. You can do this in two ways. Option 1 is to pass in the matrix to your makeAndFillMatrix method like so:

如果要创建用户输入的大小的矩阵,则不需要将n传递到makeAndFillMatrix方法中,而是需要传入大小为n的矩阵。你可以用两种方式做到这一点。选项1是将矩阵传递给makeAndFillMatrix方法,如下所示:

public static void main(String [] args)
{
    int n = getMatrixSize();
    int[][] m = makeAndFillMatrix(new int[n][n]); // new int matrix of size n
    printMatrix(m);
}

Or (the more conventional way, in my opinion), you can give makeAndFillMatrix the size of the matrix (like you are doing), and you can have makeAndFillMatrix make the array:

或者(在我看来,更传统的方式),你可以给makeAndFillMatrix矩阵的大小(就像你正在做的那样),你可以让makeAndFillMatrix创建数组:

public static void makeAndFillMatrix(int matrixSize)
{
    int[][] r = new int[matrixSize][matrixSize];
    Random generator = new Random(5);
    int rand = generator.nextInt(10);
    for(int i = 0; i < r.length; i++)
    {
        for(int j = 0; j < r[i].length; j++) //~~~I modified this line as well~~~
        {
            r[i][j]= rand;
        }
    }
    return r;
}

Anyways, you can take your pick at which approach you want to take to this problem, but regardless, I noticed that your makeAndFillMatrix method had an error. I fixed it and wrote //~~~I modified this line as well~~~

无论如何,你可以选择你想要采用哪种方法解决这个问题,但无论如何,我注意到你的makeAndFillMatrix方法有错误。我修好了它并写了// ~~~我修改了这一行~~~

Your inner for loop originally said for(int j = 0; j < r; j++), but r is a matrix. For the inner loop, you would want to increase j until it has reached the size of the current row you are on in the matrix, so I changed that line as well. (Your inner loop was correct in your printMatrix method.) Whichever approach you take for your first issue, the inner loop in your makeAndFillMatrix method will need to be modified as well.

你的内部for循环最初表示为(int j = 0; j ;>

I hope this helps!

我希望这有帮助!

#4


0  

You are trying to cast 'int' (in the main function) to an 'int[][]' (the parameter of 'makeAndFillMatrix'. Also, makeAndFillMatrix returns 'void'.

您正在尝试将'int'(在main函数中)转换为'int [] []'('makeAndFillMatrix'的参数。此外,makeAndFillMatrix返回'void'。

I believe you want to rewrite your function to something like this:

我相信你想要将你的功能改写成这样的东西:

public static int[][] makeAndFillMatrix(int r)
{
    // Create a new matrix
    int[][] matrix = new int[r][r];
    // Get a random value
    Random generator = new Random(5);
    int rand = generator.nextInt(10);
    // Assign random value to each cell of the matrix
    for(int i = 0; i < r; i++)
    {
        for(int j = 0; j < r; j++)
        {
            r[i][j] = rand;
        }
    }
    // Return the matrix
    return matrix;
}