public class Factorial{ public static void main(String[] args){ for (int i = -5; i <= 5; i++) { System.out.printf("%d! = %d ", i, negative(i)); } } // 判断n是否为负数 public static int negative(int n){ if (n >= 0){ return factorial(n); } else { return -factorial(-n); } } // 递归阶乘函数 public static int factorial(int n){ if (n == 0){ return 1; } else { return factorial(n-1) * n; } } }