سؤال

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