日志文章

2019-12-14 aflfte2011

递归方法的使用

/**
 * 递归的使用
 * @author jinhao
 *递归是指方法块自己调用自己
 */
public class TestRecursion01 {

public static void main(String[] args) {
a();
long t=System.currentTimeMillis();
//利用递归方法计算阶乘
System.out.println(b(10));
long t1=System.currentTimeMillis();
System.out.println(t1-t);
}
static int b=0;//声明一个通用变量
static void a() {
System.out.println("a");
b++;
if(b<5) {//在递归时必须设置递归头,使其按条件运行
a();
}else {
return;
}
}
static long b(int b) {
if(b==1) {
return 1;
}else {
return b*b(b-1);
}
}
}

« 包有使用于载入 | 方法语句块的使用»