//原题:使用BigInteger类计算1!+3!+5!+的前30项的和
import java.math.*;//我想使用BigInteger类来做这个题目,利用BigInteger类中的add(),compareTo(),multiply()方法做,但小子做不出啊,
public class he//我只好用另外一种方法了,只是我觉得我写的这个方法也是不够简洁,也请仙人顺带指点一二,让此方法更加简洁些;
{public static void main(String args[])
{int sum=0,i=1;
while(i<60)
{int a=1;
for(int j=1;j<=i;j++)
{
a=a*j;}
sum=a+sum;
i=i+2;
}
//BigInteger s=new BigInteger("sum");
System.out.println(sum);
}}

解决方案 »

  1.   

    BigInteger做的:
    import java.math.*;public class FactorialThrity { public static void main(String[] args) {
    BigInteger sum=new BigInteger("0");
    BigInteger peersum=new BigInteger("1");
    boolean b=true;
    for(int i=1;i<6;i++){
    peersum=peersum.multiply(new BigInteger(Integer.toString(i)));
    if(b){
    sum=sum.add(peersum);
    }
    b=!b;
    }
    System.out.println(sum);
    }}
      

  2.   

    如果是用int做的 话 直接把Integer换成int型的 就行了吧
      

  3.   

    顺带说一下,这也不是作业贴,
    大三要学java,正式接触编程,大一,大二都是基础科,暑假了,我想先在家里面练练手,这些题都是课后习题(我在学校买的别人的一本旧书)
      

  4.   

    //原题:使用BigInteger类计算1!+3!+5!+的前30项的和
    import java.math.*;//我想使用BigInteger类来做这个题目,利用BigInteger类中的add(),compareTo(),multiply()方法做,但小子做不出啊,
    public class he//我只好用另外一种方法了,只是我觉得我写的这个方法也是不够简洁,也请仙人顺带指点一二,让此方法更加简洁些;
    {    public static void main(String args[]) {
            BigInteger sum=new BigInteger("0");
            int i=30;//三十项
            Integer j=1;//当前项
            while(0<i--){
                sum=sum.add(factorial(new BigInteger(j.toString())));
                j+=2;
            }
            System.out.println(sum);
        }
        public static BigInteger factorial(BigInteger bint)
        {
            if( bint.equals(BigInteger.ZERO)||bint.equals(BigInteger.ONE))
            return BigInteger.ONE;
            return bint.multiply(factorial(bint.subtract(BigInteger.ONE)));
        }
    }