遞歸小程序之求階乘
1、題目描述
階乘 n! = n * (n-1) * (n-2) * ...* 1(n>0)
2、代碼實現
1 package com.wcy.october; 2 3 /** 4 * 時間:2016年10月23日 5 * 題目:(1)階乘 n! = n * (n-1) * (n-2) * ...* 1(n>0) 6 */ 7 public class RecursionTest5 { 8 9 public static int getResult(int n){ 10 if (n == 1) { 11 return 1; 12 }else { 13 return getResult(n-1)*n; 14 } 15 } 16 17 public static void main(String[] args) { 18 int result = getResult(5); 19 System.out.println(result); 20 } 21 }
浙公網安備 33010602011771號