Domanda

For some reason, I can't get my method to display a previously mentioned array. The only issue is in my first line. The error code is: "illegal start of expression". What am I doing wrong?

    public static void display(int matrix[][]) {
        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix[row].length; col++) {
                System.out.print(matrix[row][col] + " ");
            }
            System.out.println();
        }
    }
È stato utile?

Soluzione

your method has to be inside the class, but outside any other method.
if this method is within the main method, then your code will look something like this:

public class name
{
    public static void main(String[] args)
    {
         //code
         yourmethod(parameter); //*somewhere in your code*
    }

    public static void yourmethod(parameters p)
    {
         //code
    }
}

if it's within a regular class with no main method

public class name
{
    public name()
    {
        //code
    }

    public void yourmethod(parameters p)
    {
        //code
    }

    //other methods
}

Altri suggerimenti

The error is not in that method, surely you missed a } before

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top