奇怪的是,这是我的第一个 Java 应用程序,我想实现一个任意精度的阶乘函数,我做了递归的很好,但我的迭代的只是输出“1”,没有别的。对我来说太晚了,我不明白为什么,我不确定我哪里出错了,这里有什么明显的吗?
public static BigInteger ifact(BigInteger n) {
BigInteger ret = new BigInteger("1");
BigInteger i = new BigInteger("1");
for(i = new BigInteger("0"); i.compareTo(n) == 0; i.add(new BigInteger("1"))){
ret.multiply(i);
}
return ret;
}
如果您没有注意到它使用 BigInteger 包,请注意奇怪的文字。
另外,像 C 一样,你能做一些类似于 typedef 的事情,所以我不需要每次都输入“BigInteger”吗?
编辑:我想我的意思是设置ret
为n
,可能是这样,或者..也许不是。