【问题标题】:Modulus not positive :BigInteger模数不是正数:BigInteger
【发布时间】:2011-06-13 22:41:43
【问题描述】:

错误---模数不是正数

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)

【问题讨论】:

    标签: java biginteger modulo


    【解决方案1】:

    你正在划分你的大整数。

    让我们看看你的循环中计算了divi 的哪些值:

     i     divi[i]            zz   divi[i+1]
     0    100000000           10   10000000
     1     10000000          100     100000
     2       100000         1000        100
     3          100        10000          0
     4            0       100000          0
     5            0      1000000          0
     6            0     10000000          0
     7            0    100000000          0
     8            0   1000000000          0
     9            0  10000000000          0
    

    然后您尝试将某些内容除以 divi[4] (= 0),这显然会失败,但您发布的异常除外。

    【讨论】:

    • 嘿,谢谢,出错了,你是如何追踪 i,divi,zz, divi[i+1] 的可以分享一下代码吗
    • 我是用 Brain 1.0 做的:我只是玩翻译。但是您可以将一些System.out.println() 放在正确的位置以查看结果。或者使用调试器。
    【解决方案2】:

    看一下mod方法的定义。

    http://download.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html#mod%28java.math.BigInteger%29

    如果 m public BigInteger mod(BigInteger m) 中引发 ArithmeticException。

    m 为 0。这就是你得到异常的原因。

    【讨论】:

      【解决方案3】:

      它的意思是,模数(右侧的数字)不能为负数。必须大于 0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-13
        • 2019-04-14
        • 1970-01-01
        相关资源
        最近更新 更多