import java.util.Scanner; public class Demo1 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); int n; int res=0; while (scanner.hasNext()){ n=scanner.nextInt(); res=0; if(n>=0){ res=factorial(n); System.out.println(res); }else if(n==-1){ break; //输入-1,结束程序 } else { System.out.println("请输入非负整数!"); continue; } } scanner.close(); } public static int factorial(int n){ if (n==1||n==0){ return 1; } return n*factorial(n-1); } }