找到大于12的阶乘数时出错输出

时间:2015-09-15 09:04:16

标签: java

我正在尝试解决Java中的codechef中提供的一个factorial problem。下面是我的代码,我试过并得到数字的结果,最多12个。对于13!,我得到了错误的答案。而且我的数字输出为零。我正在学习阶段,请帮助我。

    import java.util.Scanner;

public class Factorial {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner dd = new Scanner(System.in);
        int a=dd.nextInt();
        long fact[]=new long[a];
        for (int i=0;i<a;i++)
        {
            int b = dd.nextInt();
            int factorial=0,d=0;
            if(b==0) System.out.println(1);
            for(int temp=b-1;temp>1;temp=d-1)
            {
                d=temp;
                b=b*temp;
            }
            fact[i]=b;
        }
        for(int j=0;j<a;j++)
        {
            System.out.println(fact[j]);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

13!大于max int值并且你在整数上计算它。

相关问题