n!算法的代码如下:
package interview;
public class Factorial {
public static void main(String[] args) {
long n = 5;
System.out.println(doFactorial(n));
}
public static long doFactorial(long n) {
if (n < 1) {
System.out.println("ERROR!");
return 0;
}else if (n == 1 || n == 2) {
return n;
}else {
return n * doFactorial(n -1);
}
}
}
代码执行结果如下:
网友评论