java中用while计算1到100的乘积为什么结果为0

public class Test1{ public static void main(String[] args){ int i=1; int A=1; while(i<=100){ A=i*A; i++; } System.out.println("1*2*3*4*5*6*...*100="+A); }}

//超过了整型的最大范围、。于是就是0

import java.math.*;
class Test1 {
    public static void main(String[] args) {
        int i = 1;
        BigInteger A = BigInteger.ONE;
        while (i <= 100) {
            A = BigInteger.valueOf(i).multiply(A);
            i++;
        }
        System.out.println("1*2*3*4*5*6*...*100=" + A);
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-01-03
因为数据溢出了,你用int保存存储结果,int可存储的数值范围最大也就21亿多一点,你这么连乘法,没几次就超过了本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网