Recently due to changes in the curriculum, learning plans have been changed, leaving me with less time to write blog. This evening no class, time to come to the library to write a write, a long time do not write feel a little strange!
Today and the big season to share the link to the previous blog, is about the method of nested calls and recursive calls, is the learning method in the process of the inevitable two sections, let's take a look!
about the Nested calls :
In the Java language, the methods are parallel, there is no problem with the upper-level method and the next-level method, and in the Java language a call to another method is allowed in the definition of a method, so there is a nested invocation of the method, that is, electrophoresis other methods in the called method. For example, when you execute a statement that calls a method in the main method, that is, go to execute a method, when you call the B method in the A method, and then go to execute the B method, the B method executes the breakpoint that returns the A method to continue execution, the A method executes the breakpoint that returns the Main method.
Calculate s= (2*2)!+ (3*3)!
public class demo{public static void Main (string[] args) {// int i;long s=0; for (i=2;i<=3;i++) s=s+fun1 (i); System.out.println (s); } The static long fun1 (int p) {///defines the Fun1 method used to calculate the square value and calls the Fun2 method int K;long R; k=p*p; R=fun2 (k); return r; } static long fun2 (int q) {//method for calculating factorial values fun2 int i;long c=1; for (i=1;i<=q;i++) c=c*i; return c; }}
Parsing: In the main program, the execution loop program sequentially takes the I value as the argument call method fun1 The square value of I. In the FUN1, a call to the method fun2 occurs, which is the value of the square of I as the actual argument to call Fun2, in the fun2 to complete (i*i)! The calculation. Fun2 the C value after execution (i.e. i*i! ) is returned to FUN1, which is then returned by FUN1 to the main method implementation overlay.
about the Recursive invocation :
In fact, the recursive method is directly or indirectly called its own method. Many mathematical functions are defined using recursion, and such functions can be defined and invoked in Java. Design Calculation n! Method is fun1, according to the recursive definition of n factorial, the recursive algorithm of computing fun1 can be simply described as follows:
if (n==0) return 1; else return n*fun1 (n-1);
Example: Enter a non-negative integer to display the factorial value of the number.
Import Java.util.scanner;public class demo{public static void Main (string[] args) { Scanner input = new Scanner (S ystem.in); System.out.println ("Please enter a non-negative integer:"); int n = input.nextint (); The factorial value of System.out.println (n+) is: "+fun1 (n)); } public static long fun1 (int n) { if (n==0) return 1; else return n*fun1 (n-1);} }
In fact, both nested calls and recursive calls are just extensions of the method, but the former calls another method, and the latter is the method that calls itself.
(11.06) Java trivia