Recursive invocation : by invoking or indirectly invoking the program itself
recursive invocation The most important point is that there must be a head, if there is no header , has been called down , It is a dead loop .
code example:
/*** Recursive calling code example <br> * Description: A method that calls itself directly or indirectly .<br> *@authorrushing out of the earth **/ Public classRecursion {/*** Sample program: Factorial <br> * Factorial of a number, that is, from 11 straight to that number <br> * Example: 2! = 1*2 5! = 1*2*3*4*5<br> * Decomposition: 5! = 5*4! = 5*4*3! = 5*4*3*2! = 5*4*3*2*1<br> *@returnreturns the product of the factorial of I and i-1, i.e. i* (i-1)! * */ Private Static intGetfactorial (inti) { if(i = = 1) { return1; } Else { returnI*getfactorial (i-1); } } /*** Sample Program: Add <br> * Randomly specify a number, the sum from 1 to this number and <br> * Example: 5 of the cumulative = 5+4+3+2+1<br> * Decomposition: 5 of the cumulative = 5+4 tired additive = 5+4+3 = 5+4+3+2+1<br> *@authorrushing out of the earth *@paramThe number to which I accumulate *@seerecursion#getsumbycirculation (int) cumulative for loop method **/ Private Static intGetsumbyrecursion (inti) { if(i = = 1) { return1; } Else { returni + getsumbyrecursion (i-1); } } /*** Cumulative for Loop method <br> *@authorrushing out of the earth *@paramThe number to which I accumulate *@seerecursion#getsumbyrecursion (int) Cumulative recursive invocation method **/ Private Static intGetsumbycirculation (inti) { intresult = 0; for(intj = 1; J <= I; j + +) {result+=J; } returnresult; } /*** Main program entrance **/ Public Static voidMain (string[] args) {//ask for factorial of 5 intFACTORIAL5 = getfactorial (5); System.out.println (The factorial of 5 is: "+FACTORIAL5); //use the For loop to get the 100 accumulation intsumByCirculation100 = getsumbycirculation (100); System.out.println (The cumulative value of 100 calculated using the For loop is: "+sumByCirculation100); //use recursive calls to get 100 of the cumulative intsumByRecursion100 = getsumbyrecursion (100); System.out.println ("The cumulative value of 100 calculated with recursive invocation is:" +sumByRecursion100); } }
[Java Basics] Recursive invocation