使用while语句,计算整数n的阶乘
 N的阶乘计算表示 n!=n*(n-1)*(n-2)…2*1

解决方案 »

  1.   

    public class Jie{
     public static void main(String[] args) throws IOException{
      System.out.println("请输入一个数:");
      int one=System.in.read();
      int sum=1;
      for(int i=one;i>0;i--){
       sum*=i;
      } 
      System.out.println(one+"!"+"="+sum);
     }
    }
      

  2.   

    import java.math.BigInteger;
    public class Test1 {
    public static void main(String[] args) {    
          
        System.out.println(caculate(22));   
       
        System.out.println(caculate2(20));   
       
    }   
      
    /**  
     * n!(当n>20时采用此方法)  
     * @param n  
     * @return  
     */  
    public static String caculate(int n) {   
        if (n < 0) {   
            System.out.println("n必需大于0");   
            return "error";   
        } else if (n == 0 || n == 1) {   
            return "1";   
        } else {   
         //BigInteger multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger。 
            return BigInteger.valueOf(n).multiply(new BigInteger(caculate(n-1))).toString();   
        }   
    }   
      
    /**  
     * n!(当n<=20时采用此方法)  
     * @param n  
     * @return  
     */  
    public static long caculate2(long n) {   
        if (n < 0) {   
            return 0;   
        } else if (n == 0 || n == 1) {   
            return 1;   
        } else {   
            return n * caculate2(n -1);   
        }   
    }  }
      

  3.   


    public class Test { public static int fact(int n)
    {
    while (n!=0)
    {
    return n*fact(n-1);
    }

    return 1;

    }
    public static void main(String args[])
    {
    System.out.println(fact(3));
        
    }

    }
      

  4.   

    public class jie {
    public static void main(String args[])
    {
    int n=5,i=1,z=1,k=1;
    while(k<=n)
    {
    z*=k;
    k++;
    }
    System.out.println("n!="+z);
    }
    }不好意思啊
    刚才看错了
    给你发错了
    这个绝对够简单
    够明了
      

  5.   

    前不久正好写了个求1~n的阶乘之和的,不过用的for循环,你可以大概看看import java.math.*;
    import java.util.*;public class TestBigIntegerClass {

    public static void main(String[] args) {
    BigInteger value , result , sum;
    int m;
    sum = BigInteger.valueOf(0);
    result = BigInteger.valueOf(1);
    Scanner in = new Scanner(System.in);
    System.out.println("取n值:");
    m = in.nextInt();
    for(long i=1;i<=m;i++) {
    value = BigInteger.valueOf(i);
    result = result.multiply(value);
    //sum =sum.add(result); 

    }

    System.out.println("n的阶乘为: "+ result);
    }}
      

  6.   

       fact(int n) {  
            if(n == 0 || n == 1)   
                return 1;  
               else   
                    return n * fact(n - 1);  
          }  
    这样也行呀...个人觉得挺方便的..hehe
      

  7.   

    楼上只用int类型的把n=100代进你们的方法里看能不能得到你们想要的结果
      

  8.   

    刚想分辨几句,发现自己写的程序连n=20都算不出正确结果。算了,改吧。import java.math.BigInteger;public class Test { public static BigInteger fact(BigInteger n)
    {
    while (!n.equals(BigInteger.ZERO))
    {

    return n.multiply(fact(n.subtract(BigInteger.valueOf(1))));
    }

    return BigInteger.valueOf(1);

    }
    public static void main(String args[])
    {
    BigInteger b=BigInteger.valueOf(100);
    System.out.println(fact(b));

    }

    }
      

  9.   

    只用while(),还需要递归吗?int m = 1;
    if(n > 0)
    {
       while(n)
       {
          m *= n;
          --n;
       }
    }
    else
        printf("input error\n");
      

  10.   

    优化一下:int m = 1;
    if(n >= 0)
    {
       int temp = n;
       while(n)
       {
          m *= n;
          --n;
       }
       printf("%d! = %d\n",temp,m);
    }
    else
       printf("input error\n");