int factorial(int n)
{
  if (n==0 || n==1)
     return 1;
  else
     return n*factorial(n-1);
}

解决方案 »

  1.   

    to:lifeis(人就是直立行走的驴) :
    你那个程序是C下的吧。
    我拿到java里面,不能通过啊!
    D:\JavaProject\Example0409\SCJPExam01.java:32: static 不能把变量Sum设置成static context。
      Sum = factorial(X);to:stchin(书剑恩仇) 
    我也是很久以前学的,现在也忘了,呵呵,没得昏了!
      

  2.   

    class Factorial
    {
    public static void main(String[] args) 
    {
    int n = 5; //假设输入的数是 5
    int sum = 1; if (n < 0)
    {
    System.out.println("n is overflow");
    return ;
    }
    else if (n == 0)
    {
    n = 0;
    }
    else {
    for (int i = 1; i <= n ; i ++)
    {
    sum = sum * i;
    }
    } System.out.println("n! = " + sum);
    }
    }不过此种方法不适用于 n 很大的数。如果你确实需要用到极大的 n 的话,可以和我联系。我会尽力帮忙。qq:2399535
      

  3.   

    public class Shit{
    public static void main(String[] args){
    System.out.print(factorial(4));
    }public static int factorial(int n){
      if (n==0 || n==1)
         return 1;
      else
         return n*factorial(n-1);
    }
    }//通过编译 呵呵
      

  4.   

    很大的话可以考虑用java.math.BigInteger来处理,再大的数它都能处理
      

  5.   

    to:sun_gui(痛哭的人) 
    确实通过了,给分谢谢george_yingjun(竹子) 帮忙,给分。lifeis(人就是直立行走的驴)的例子也有帮助,给分。
    ok暂时告一段落。