代码:
package 递归;
import java.util.Scanner;
public class Foctorial {
public int foctorial(int n) {
if(n==1) {
return 1;
}
else return n*foctorial(n-1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Foctorial foctorial = new Foctorial();
System.out.println("请输入一个数:");
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
try {
int result = foctorial.foctorial(N);
System.out.println(result);
} catch (Exception e) {
e.getStackTrace();
}
}
}
测试结果: