質問

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();
        }
    }
役に立ちましたか?

解決

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
}

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top