给你个完整的源程序,具体n在运行时作为命令行参数输入。public class Test { public static long f(int n) { if(n==0 || n==1) return 1; return n*f(n-1); } public static void main(String[] args) throws Exception { int n = Integer.parseInt(args[0]); System.out.println(n+"! = "+f(n)); } }
{
if(n==1)
{
return 1;
}
return getResult(n-1)*n;
}
如果有需要,也可以改用long
public static long f(int n) {
if(n==0 || n==1)
return 1;
return n*f(n-1);
}
public static void main(String[] args) throws Exception {
int n = Integer.parseInt(args[0]);
System.out.println(n+"! = "+f(n));
}
}