Java方法的递归

使用方法同C语言中的递归
递归需要有两部分:1.递归结束的条件;2.递归的函数体
简单的代码如下

public class HelloJava {
public static void main(String[] args) {
   int n= 1000;
   System.out.println(summary(n));
}
public static int summary(int n){
    if(n== 1) return 1;
    else {
        int i = n + summary(n - 1);
        return i;
    }
}
}

猜你喜欢

转载自www.cnblogs.com/loneykids/p/11587820.html