递归使用

public class Test {

 public Test() {
 }
 /**
  *
  * @author wb
  * @param i 参数i代表开始递归数字
  * @return
  * 该方法是用来计算数的阶乘
  */
 public static int method(int i){
  if(i==1){
   
   return 1;
  }else{
   
   return i*method(i-1);
  }
  
 }
 
 /**
  * @param i
  * @return
  * 该方法是用来计算数的平方和
  */
 public static long methodSquare(int i){
  if(i==1){
   return 1;
  }
  return i*i+methodSquare(i-1);//1+4+9+16+25+36=91
 }

 public static void main(String args[]){
  System.out.println(method(5));
  System.out.println(methodSquare(100));
  
 }
}

原文地址:https://www.cnblogs.com/missjava/p/3162502.html