java中预期的标识符

Aja*_*rni 0 java

我在java中编写了一个简单的程序:

public class Will {

    int static square(int x) {
        int y = x * x;
        return y;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a number...");
        int n = sc.nextInt();
        int result = Will.square(n);
        System.out.println("Square of " + n + " is " + result);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我收到了这些错误:

square.java:6: error: <identifier> expected
         int static square (int x)
            ^
square.java:6: error: invalid method declaration; return type required
         int static square (int x)
                    ^
2 errors
Run Code Online (Sandbox Code Playgroud)

如何解决这些问题?

Kon*_*kov 8

它一定要是:

static int square (int x)
Run Code Online (Sandbox Code Playgroud)

即返回类型必须在访问修饰符之后和方法名称之前.