1 package com.Lucky.base; 2 3 import java.util.Scanner; 4 5 public class RecursionMethod { 6 /* 7 递归见解: 自己调用自己 8 */ 9 public static void main(String[] args) { 10 11 //调用阶乘方法 12 int re= recursion(3); 13 System.out.println(re); 14 15 16 //调用add 17 //add(); 18 subtraction(20,10,20,50); 19 //multiplication(); 20 } 21 22 23 //案例1:阶乘 例如:5!=5*4*3*2*1 24 25 /** 26 * 阶乘方法【递归的应用】 27 * @return 28 * 见解: 29 * n=1时 直接输出1 30 * n=2时 res=2*recursion(1) recursion(1)=1 31 * n=3时 res=3*recursion(2) recursion(2)=2*recursion(1) recursion(1)=1 32 * …………………… 33 */ 34 public static int recursion(int n) { 35 int res=0; 36 if(n==1){ 37 res=1; 38 }else{ 39 res=n*recursion(n-1); 40 } 41 return res; 42 } 43 44 ///////////////////作业1:加减乘除【四合一算法】///////////////////////// 45 public static int add() { 46 int sum=0; 47 48 //添加扫描器 49 Scanner scanner=new Scanner(System.in); 50 System.out.println("请输入加法数据"); 51 while (scanner.hasNextInt()){ 52 int se=scanner.nextInt(); 53 sum+=se; 54 55 } 56 57 System.out.println(sum); 58 scanner.close(); 59 60 61 return sum; 62 } 63 64 public static int subtraction(int... abs) { 65 int leth=abs[0]; 66 int sum=0; 67 //遍历数据 68 for (int i = 1; i < abs.length; i++) { 69 sum=leth-abs[i]; 70 leth=sum; 71 } 72 System.out.println(sum); 73 return sum; 74 } 75 76 77 78 }
标签:递归,int,recursion,sum,System,res,public From: https://www.cnblogs.com/Lucky-only/p/16891842.html