递归小Demo

public class demo5 {

public static void main(String[] args) {
        //初始值为100
         int n = 100;
       //调用number方法,返回一个int类型的值
         int num = number(n);
       //打印结果
         System.out.println("(1-100)相加的和为:"+num);    // 结果 5050
  }

       //递归调用
public static int number(int n){
          if (n == 1) {
          return 1;
          }else {
                   return n+number(n-1);
         }
     }
}

原文地址:https://www.cnblogs.com/mumuda/p/6132723.html