模数非正数:BigInteger
错误 ---模数不是正数
BigInteger 取值 0 或 -ve,但我无法弄清楚
public int[] conCheck(BigInteger big)
{
int i=0,mul=1;
int a[]= new int[10];
int b[]= new int[10];
BigInteger rem[]= new BigInteger[11];
BigInteger num[]= new BigInteger[11];
String s="100000000";//,g="9";
//for(i=0;i<5;i++)
//s=s.concat(g);
BigInteger divi[]= new BigInteger[11];
divi[0]=new BigInteger(s);
num[0]=big;
for(i=0;i<10;i++)
{
int z = (int)Math.pow((double)10,(double)(i+1));
BigInteger zz = new BigInteger(String.valueOf(z));
divi[i+1]=divi[i].divide(zz);
num[i+1]=num[i].divide(zz);
}
{ for(i=0;i<10;i++)
{
rem[i] = num[i].mod(divi[i]);
b[i]=rem[i].intValue();
if(i>=4)
{
mul= b[i]*b[i-1]*b[i-2]*b[i-3]*b[i-4];
}
a[i]=mul;
}
}
return a;
}
控制台上的错误在哪里
C:\jdk1.6.0_07\bin>java euler/BigConCheck1
Exception in thread "main" java.lang.ArithmeticException: BigInteger: modulus no
t positive
at java.math.BigInteger.mod(BigInteger.java:1506)
at euler.BigConCheck1.conCheck(BigConCheck1.java:31)
at euler.BigConCheck1.main(BigConCheck1.java:65)
error ---Modulus not positive
BigInteger is taking vaule 0, or -ve, but i can't figure out where
public int[] conCheck(BigInteger big)
{
int i=0,mul=1;
int a[]= new int[10];
int b[]= new int[10];
BigInteger rem[]= new BigInteger[11];
BigInteger num[]= new BigInteger[11];
String s="100000000";//,g="9";
//for(i=0;i<5;i++)
//s=s.concat(g);
BigInteger divi[]= new BigInteger[11];
divi[0]=new BigInteger(s);
num[0]=big;
for(i=0;i<10;i++)
{
int z = (int)Math.pow((double)10,(double)(i+1));
BigInteger zz = new BigInteger(String.valueOf(z));
divi[i+1]=divi[i].divide(zz);
num[i+1]=num[i].divide(zz);
}
{ for(i=0;i<10;i++)
{
rem[i] = num[i].mod(divi[i]);
b[i]=rem[i].intValue();
if(i>=4)
{
mul= b[i]*b[i-1]*b[i-2]*b[i-3]*b[i-4];
}
a[i]=mul;
}
}
return a;
}
Error as on Console
C:\jdk1.6.0_07\bin>java euler/BigConCheck1
Exception in thread "main" java.lang.ArithmeticException: BigInteger: modulus no
t positive
at java.math.BigInteger.mod(BigInteger.java:1506)
at euler.BigConCheck1.conCheck(BigConCheck1.java:31)
at euler.BigConCheck1.main(BigConCheck1.java:65)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
顾名思义,模数(右侧的数字)不能为负。它必须大于 0
It means what it says, the modulus (the number on the right hand side) must NOT be negative. It must be greater than 0
您正在除以大整数。
让我们看看在您的循环中计算了
divi
的值:然后您尝试将某些内容除以 divi[4] (= 0),这显然会失败,并出现您发布的异常。
You are dividing your big integers.
Let's see what values of
divi
are calculated in your loop:And then you try to divide something by divi[4] (= 0), which will obviously fail with the exception you posted.
看一下mod方法的定义。
http ://download.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html#mod%28java.math.BigInteger%29
如果 m <= 0。m
为 0,则在
public BigInteger mod(BigInteger m)
中抛出 ArithmeticException。这就是您收到异常的原因。Take a look at the definition of the mod method.
http://download.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html#mod%28java.math.BigInteger%29
It throws an ArithmeticException in
public BigInteger mod(BigInteger m)
if m <= 0.m is 0. Thats why you are getting the exception.