public class JieCheng {
public static void main(String[] args) {
int i = 5;
int jiecheng = jc(i);
System.out.println("普通方法算得"+i+"阶乘是:"+jiecheng);
int jie = j(i);
System.out.println("使用递归算得阶乘是:"+jie);
}
//不用递归
private static int jc(int in) {
int j = 1;
for(int i=1;i<=in;i++){
j *= i;
}
return j;
}
//使用递归方法
private static int j(int i) {
int m;
if(i==1){
m = i;
}else {
m = j(i-1)*i;
}
return m;
}
} |
|