我在这里有这个代码:
public class Main {
public static void main(String[] args) {
System.out.println(Math.round(12.5));
System.out.println(round(12.5));
}
public static double round(double integer) {
return Math.round(integer);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,它输出:
13
13.0
Run Code Online (Sandbox Code Playgroud)
为什么当我Math.round()在main方法中正常运行时,它提供一个整数值,而在“round”方法中提供一个double值?我知道我的方法是“double”类型,但 Java 不允许我将其更改为“int”。这背后有什么原因吗?谢谢。