public static void main(String []args){
getN(5);
System.out.print(getN(5));
public static int getN(int n){
int result;
if(n=1){
result=1;
}else{
result =n*getN(n-1);
}
return result;
递归不用循环,调用自身循环,上诉代码为递归,它的普通形式如下:
public static void main(String []args){
System.out.print(getN(5));
}
public static int getN(int n){
int result=1;
for(int i=1;i<=n;i++){
result = result*i;
}
return result;
}
}