帮做一道JAVA编的求n!的阶层,用do while语句,谢谢各位

解决方案 »

  1.   

    简单写一个看看,不保证没语法错误,呵呵public static long test(long n){
      long rtn = 1;
      do{
        rtn *= n;
      }while(--n > 0);
      return rtn;
    }
      

  2.   

    看我的代码:package com.sytdc.cxl;public class JieChenTest { public static void main(String args[]) { System.out.println(" if-----   " + IfTest(5)); System.out.println(" while-----   " + WhileTest(5)); System.out.println(" dowhile-----   " + DoWhileTest(5));

    System.out.println(" ForTest-----   " + ForTest(5));
    } public static int WhileTest(int m) { if (m == 0 || m == 1) {
    return 1;
    } int i = 1;
    int s = 1;
    while (i <= m) {
    s *= i;
    i++;
    }
    return s;
    } public static int DoWhileTest(int m) { if (m == 0 || m == 1) {
    return 1;
    } int i = 1;
    int s = 1;
    do {
    s *= i;
    i++;
    } while (i <= m);
    return s;
    } public static int ForTest(int m) {

    if (m == 0 || m == 1) {
    return 1;
    }

    int s= 1;
    for(int i=1;i<=m;i++){
    s*=i;
    }
    return s;
    }




    public static int IfTest(int m) { if (m == 0 || m == 1) {
    return 1;
    } else {
    return m * IfTest(m - 1);
    } }}
    运行结果:
     if-----   120
     while-----   120
     dowhile-----   120
     ForTest-----   120