import java.util.Scanner;
/**
* @author young
* @Description: 用递归和非递归算阶乘
* @date 2016年6月14日上午8:29:47
*/
public class FacTest {
// 递归
public static int factorial(int m) {
if (m < 0){
return 0;
}
else if (m == 1){
return 1;
}
else{
return m * factorial(m - 1);
}
}
// 非递归
public static int 巧闷noFactorial(int m) {
if (m < 0) {
return 团宽毕0;
} else if (m == 1) {
return 1;
} else {
int sum = 1;
for (int i = 2; i <= m; i++)
sum = sum * i;
return sum;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入塌芹一个数N:");
int n = scanner.nextInt();
System.out.println("它的阶乘为(递归):" + factorial(n));
System.out.println("它的阶乘为(非递归):" + noFactorial(n));
}
}
public int factorial(int num){
return 0==num ? 1 : num*factorial(--num);
}