1 public class Factorial {
 2 
 3     public static void main(String[] args) {
 4         // 测试一下计算9的阶乘
 5         int n = 9;
 6         int result = factorial(n);
 7         System.out.println(n + "的阶乘为:" + result);
 8     }
 9 
10     // 计算阶乘的方法
11     public static int factorial(int n) {
12         if (n == 1) {// 递归头(何时结束递归)
13             return 1;
14         } else {// 递归体(何时调用方法自己本身)
15             return n * factorial(n - 1);
16         }
17     }
18 
19 }

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。