gpt4 book ai didi

java - java中期望的标识符

转载 作者:行者123 更新时间:2023-12-01 06:51:05 25 4
gpt4 key购买 nike

我用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

如何解决这些问题?

最佳答案

它必须是:

static int square (int x)

即返回类型必须位于访问修饰符之后、方法名称之前。

关于java - java中期望的标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28737769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com