萌新驾到,话不多说,直接上代码,希望您能有所收获
//导入相关包 import javax.swing.*; import java.util.Scanner; public class exercise { public static void main(String args[]) { //键盘输入 Scanner input = new Scanner(System.in); int number = input.nextInt(); System.out.print("请输入您要求得阶乘:"); //调用方法并打印 int i = factorial(number); System.out.println(i); } //先判断n的范围,之后再进行递归 public static int factorial(int n){ if (n == 1 || n == 0) return 1; return n*factorial(n-1); } }