日期:2014-05-20  浏览次数:20732 次

麻烦高手解决小程序的稳定性*/40分/*
今天朋友讨论的一个小代码   输入小的数字   还可以运行   大一点的数字就会报错
请问大家该如何修改?!谢谢!换一种更好的写法也可以!

public   class   jianmo   {
    static   long   count   =   0;

    public   static   long   jech(long   a)   {
        long   s   =   1;
        for   (long   i   =   a;   i   >   0;   i--)
            s   *=   i;
        return   s;

    }

    public   static   void   main(String[]   args)   {
        int   n   =   Integer.parseInt(args[0]);
        int   t;
        int   x;
        int   y;
        jianmo   j   =   new   jianmo();
        for   (t   =   0;   t   <=   n;   t++)
            for   (x   =   0;   x   <=   n;   x++)
                for   (y   =   0;   y   <=   n   /   2;   y++)   {
                    if   (x   +   2   *   y   ==   n   &   x   +   y   ==   t)   {
                        try{

                        count   =   count   +   j.jech(t)   /   (j.jech(t   -   x)   *   j.jech(x));

                        }catch(ArithmeticException   e){
                            System.out.println(e.getMessage());
                        }
                     
                        //   System.out.println(x   +   ", "   +   y   +   ", "   +   count+ ", "+t);
                    }
                }
        System.out.println(count);
    }
}

------解决方案--------------------
用BigInteger代替你的int和long。
具体参见jdk
------解决方案--------------------
越界了当然报错了。唉!
------解决方案--------------------
越界→出错→用java.math.BigInteger类来替换