IO流的递归概述

递归:方法定义中调用方法本身的现象。
注意事项:

A:递归一定要有出口,否则就是死递归
B:递归的次数不能太多,否则就内存溢出
C:构造方法不能递归使用

需求:5的阶乘

5!=5*4*3*2*1;
5!=5*4!;
public class test {
	public static int resault(int n){
		int sum=1;
			if(n==1)
				return 1;
			sum=n*resault(n-1);
	
		return sum;
	}
public static void main(String[] args) {
	//第一个,循环实现
	int xx=1;
	for(int i=2;i<=5;i++){
		xx*=i;
	}
	System.out.println(xx);
	
	//第二种,阶乘
	System.out.println(resault(5));
}
}
发布了188 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Ting1king/article/details/104969916