利用递归求阶乘

 1 package com.d;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Digui {
 6 
 7     public static void main(String[] args) {
 8         Digui d = new Digui();
 9 
10         System.out.println("请输入一个整数");
11         Scanner scan = new Scanner(System.in);
12         int a = scan.nextInt();
13         System.out.println(a + "!=" + d.jiecheng(a));
14     }
15 
16     // 递归求阶乘
17     public int jiecheng(int a) {
18         if (a == 1 || a == 0) {
19             return 1;
20         } else {
21             return a * jiecheng(a - 1);
22         }
23     }
24 }

 

posted @ 2016-08-18 21:45  YangJavaer  阅读(379)  评论(0编辑  收藏  举报