集册 Java实例教程 带递归解的经典阶乘

带递归解的经典阶乘

欢马劈雪     最近更新时间:2020-01-02 10:19:05

413
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
带递归解的经典阶乘
//from 时代Java公众号 - nowjava.com

public class Main {

  public static void main(String[] args) {

    int n = 5;

    long fact;

    fact = factorial(n);

    System.out.println("The factorial of " + n + " is " + fact + ".");

  }


  private static long factorial(int n) {

    if (n == 1)

      return 1;

    else

      return n * factorial(n - 1);

  }


}// from 时 代 J a v a 公 众 号