Java 递归函数返回类型错误
我编写了一个递归函数,将Java 递归函数返回类型错误,java,recursion,biginteger,Java,Recursion,Biginteger,我编写了一个递归函数,将int作为参数,并返回一个biginger。似乎没什么问题,但有一行用红色下划线 我很抱歉,如果这是如此明显,但目前我不能围绕这一点我的头 public static BigInteger factorial(int a){ BigInteger tot=BigInteger.valueOf(a); if(a>1)tot*=factorial(a-1); return tot; } 如果(a>1)tot*=
int
作为参数,并返回一个biginger
。似乎没什么问题,但有一行用红色下划线
我很抱歉,如果这是如此明显,但目前我不能围绕这一点我的头
public static BigInteger factorial(int a){
BigInteger tot=BigInteger.valueOf(a);
if(a>1)tot*=factorial(a-1);
return tot;
}
如果(a>1)tot*=(阶乘(a-1)),则行if代码>以红色下划线
错误:
必需:BigInteger,找到:int
同时:
public static void main(String[] args) {
BigInteger a=f(8);
}
public static BigInteger f(int a){
BigInteger g=BigInteger.valueOf(a);
return g;
}
…有效
它来自Intellij Idea。是否有一些基本的和重要的概念我遗漏了?多谢各位
<> > >编辑< /强>:因此,正如用户CeleB45解释的那样,java <强>不支持操作符重载< /强>,不像C++,这对我来说更熟悉。如果有人发现这是重复的,请删除,我表示歉意 *=
不适用于BigInteger。改用:
if (a > 1) tot = tot.multiply(factorial(a - 1));
Java不支持运算符重载,因此不能对其使用乘法*
运算符。相反,您应该使用:
公共BigInteger乘法(BigInteger val)
biginger
s上的所有基本数学运算都必须使用相应的