欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

JAVA递归,

来源: javaer 分享于  点击 15578 次 点评:17

JAVA递归,


递归调用指在方法执行的过程中,出现该方法本身的调用



1.找到递归的出口

2.找到递归关系式


例子1:


public class jiecheng{
	public static void main(String args[]) {
		
		System.out.println(method(5));
	}
	public static int method(int x) {
		if(x == 1)
			return 1;
		else
			return x*method(x - 1);
	}
}


例子2:

public class HelloWorld{
	public static void main(String args[]) {
		
		System.out.println(method(5));
	}
	public static int method(int x) {
		if(x == 1 || x == 2)
			return 1;
		else
			return method(x - 1) + method(x - 2);
	}
}


相关文章

    暂无相关文章
相关栏目:

用户点评