递归 *用递归算法算阶乘*

 1 /**
 2  * 测试递归算法
 3  * 
 4  *
 5  */
 6 public class TestRecursion {
 7     static int a = 0;
 8     public static void test01(){
 9         a++;
10         if(a<=10){
11             System.out.println("test01:"+a);
12             test01();
13         }
14         else{
15             System.out.println("over");
16         }
17     }
18     public static void test02(){
19         
20     }
21     public static long factorial(int n){
22         if(n==1){
23             return 1;
24         }else{
25             return n*factorial(n-1);
26         }
27     }
28     public static void main(String[] args){
29         test01();
30         System.out.println("#############");
31         long k = factorial(5);
32         System.out.println(k);
33         System.out.println(factorial(5));
34     }
35 
36 }

 

posted @ 2018-02-07 23:56  zbgghost  阅读(126)  评论(0编辑  收藏  举报