阶乘用到递归- public class JieChengDemo {
- static int sum=1;
- public static void main(String[] args) {
- int num=2;
- jieCheng(num);
- System.out.println("jiecheng"+num+"!="+sum);
- }
- public static int jieCheng(int num) {
- if (num!=0) {
- sum=num* jieCheng(num-1);
- }
- return num;
- }
- }
复制代码 |
|