使用BigInteger时获取Numberformatexception



我正在尝试使用方法(m+n)解决Project Euler问题15/(m) !*(n) !其中m&n是网格的长度&宽度

我已经将阶乘的所有值转换为BigInteger,仍然在运行时显示错误。。帮助

import java.util.*;
import java.math.*;

public class Problem15 {
public static void main (String[] args) {
int length = getLength();
int width = length;
BigInteger Lfact = new BigInteger("fact(length)");
BigInteger Wfact = new BigInteger("fact(width)");
BigInteger LWfact = new BigInteger("fact(Length+width)");
BigInteger Denom = Lfact.multiply(Wfact);
System.out.println("For grid size "+length+"X"+width+" the total no of routes are "+LWfact.divide(Denom));
}
private static int getLength() {
System.out.print("Please enter grid length - ");
Scanner inp1 = new Scanner (System.in);
int length = inp1.nextInt();
inp1.close();
return length;  
}
public  BigInteger fact(int num) {
BigInteger result = new BigInteger("1");
for(int i = num; i > 1; i--) {
result = result.multiply(BigInteger.valueOf(i));
}
return result;
}
}

阅读BigInteger:的Javadoc

公共BigInteger(字符串val)

将BigInteger的十进制字符串表示形式转换为BigInteger。字符串表示由可选的减号组成后面跟着一个或多个十进制数字序列的符号。这个字符到数字的映射由character.digit提供。字符串可能不包含任何无关字符(例如空白)。

很明显,您使用构造函数是非法的,错误消息告诉我们:

NumberFormatException。。。。BigInteger。(未知来源)问题15.main(问题15.java:15)

最新更新