利用递归求阶乘:
public class MyTest {
public static void main(String[] args) {
System.out.println(Jc(getInt()));
}
public static long Jc(int n){
long su=1;
if(n>1){
su=n*Jc(n-1);
}
return su;
}
public static int getInt(){
boolean flag=false;
int result=0;
Scanner input=new Scanner(System.in);
do {
try {
System.out.print("请输入数字:");
result=input.nextInt();
flag=false;
} catch (Exception e) {
System.out.println("输入错误!");
input.nextLine();
flag=true;
}
} while (flag);
return result;
}
}