Java算法题之求阶乘
import java.util.Scanner;
/**
* 求阶乘的方法,2种方法,第一种循环遍历,第二种递归
* 下面使用递归方法
*/
public class test {
public static int fun(int num) {
int result = 1;
if(num > 1) {
result = num * fun(num - 1);
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int num = sc.nextInt();
System.out.println(fun(num));
}
}
}