java 方法调用及测试递归算法 练习

package lianXi;
/**

  • Recursion递归
  • 测试递归算法
  • @author Administrator

*/
public class TestRecursion {
public static void test01(){
System.out.println(“我是王伟”);
test02();
}
public static void test02(){
System.out.println(“很高兴认识大家”);
test03();
}
public static void test03(){
System.out.println(“大家好!”);
}
//递归算法
static int a = 0;
public static void test(){
a++;
System.out.println(a);
if(a<=10){//满足条件调自己;递归头
test();
}else{//不满足条件停止调用;递归体
System.out.println(“停止执行”);
}
}
//算阶乘

private static long factorial(int n){
	if(n==1){
		return 1;
	}else {
		return n*factorial(n-1);	
	}
}
public static void main(String[] args) {
	test01();//调用
	System.out.println("---------");
	test();
	System.out.println("**********");
	//factorial(5);
	System.out.println(factorial(5));
	
}

}
//-----------------------------------结果
我是王伟
很高兴认识大家
大家好!

1
2
3
4
5
6
7
8
9
10
11
停止执行


120

发布了174 篇原创文章 · 获赞 7 · 访问量 8410

猜你喜欢

转载自blog.csdn.net/weixin_45339692/article/details/105399527