java中预期的标识符

时间:2015-02-26 08:40:00

标签: 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);
    }
}

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

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

如何解决这些问题?

2 个答案:

答案 0 :(得分:8)

必须是:

static int square (int x)

,即return-type必须在access-modifiers之后和方法名之前。

答案 1 :(得分:1)

改变它

public/private/protected(Access specifier) static int square(int x) 

根据JAVA standard rules