public class Test{
   public static void main(String[] args) {
      try{
int input = Integer.parseInt(args[0]);
String result = factorial(input);
System.out.println(result);
  }catch( Exception e ){
System.out.println( e );
  }
   }
   public static String factorial(int x) {     
StringBuffer rs = new StringBuffer("1");
    for (int i = 1; i <= x; i++) {
        StringBuffer tmp_rs = new StringBuffer();
        int highValue = 0;
        for (int j = 0; j < rs.length(); j++) {
            char cNum = rs.charAt(j);
            int nNum = Integer.parseInt(String.valueOf(cNum));
            int value = i * nNum + highValue;
            int lowValue = value % 10;
            highValue = value / 10;
            tmp_rs.append(lowValue);
        }
        if (highValue != 0)
            tmp_rs.append(highValue);
        rs = tmp_rs;
    }
return rs.reverse().toString();
   }
}

解决方案 »

  1.   

    import java.math.*;
    import java.util.*;public class Factorial4 
    {
       public static void main(String[] args) 
       {
          int input = Integer.parseInt(args[0]);
          int result = factorial(input);
          System.out.println(result);
       }
       public static BigInteger factorial(int x) 
       {     
          if(x < 0) 
          {
           throw new IllegalArgumentException("x must be non-negative.");
          }
          else
          {
          BigInteger fact;
          for(int i = 1; i < x; i++) 
          {
            fact = (BigInteger.valueOf(i)).multiply((BigInteger.valueOf(1)));
          }
          
          return fact;
          }
       }
    }
      

  2.   

    import java.math.*;
    import java.util.*;public class Factorial4 
    {
       public static void main(String[] args) 
       {
          int input = Integer.parseInt(args[0]);
          int result = factorial(input).intValue();
          System.out.println(result);
       }
       public static BigInteger factorial(int x) 
       {     
          if(x < 0) 
          {
           throw new IllegalArgumentException("x must be non-negative.");
          }
          else
          {
          BigInteger fact = BigInteger.valueOf(0);
          for(int i = 1; i < x; i++) 
          {
            fact = (BigInteger.valueOf(i)).multiply((BigInteger.valueOf(1)));
          }
          
          return fact;
          }
       }
    }调试通过了。
    环境: jdk1.4,Win2k Advance Server.
      

  3.   

    是个好程序,我研究一下...
    不过能介绍一个用BigInteger实现的方法吗!
      

  4.   

    to:acefr() 
    你的程序调试是通过了,但并没有实现阶乘的功能..
    只需稍加改动就可以了。
    不过还是要十分的感谢大家.
    结贴~~```import java.math.BigInteger;
    import java.util.*;public class Factorial4 {
       protected static ArrayList table = new ArrayList();
       static {
         table.add(BigInteger.valueOf(1));
       }
       public static void main(String[] args) {
         int input = Integer.parseInt(args[0]);
         BigInteger result = factorial(input);
            System.out.println(result);
       }
       public static synchronized BigInteger factorial(int x) {     
          if(x < 0) throw new IllegalArgumentException("x must be non-negative.");
          for(int i = 1; i <= x; i++) {
            BigInteger last = (BigInteger)table.get(i-1);
            BigInteger fact = last.multiply(BigInteger.valueOf(i));
            table.add(fact);
          }
          
          return (BigInteger) table.get(x);
       }
    }