Recursive Fibonacci series java three methods, Fibonacci java
The examples in this article share the code of the java recursive Fibonacci series for your reference. The details are as follows:
First, common writing
public class Demo { public static void main(String[] args) { int num1 = 1; int num2 = 1; int num3 = 0; System.out.println(num1); System.out.println(num2); for (int i = 1; i < 10; i++) { num3 = num1 + num2; num1 = num2; num2 = num3; System.out.println(num3); } } }
Method 2: array-form Recursion
public class DIGUI1 { public static void main(String[] args) { int []arr=new int[20]; arr[1]=1; arr[2]=1; System.out.print(" "+arr[1]); System.out.print(" "+arr[2]); for(int i=3;i<20;i++){ arr[i]=arr[i-1]+arr[i-2]; System.out.print(" "+arr[i]); } } }
Method 3: recursive writing
Public class Demo {public static int f (int n) throws Exception {if (n = 0) {throw new Exception ("parameter error! ");} If (n = 1 | n = 2) {return 1;} else {return f (n-1) + f (n-2 ); // call yourself} public static void main (String [] args) throws Exception {for (int I = 1; I <= 10; I ++) {System. out. print (f (I) + "");}}}
The biggest problem with recursion is efficiency, but some programs must be written recursively. For example, the famous hanruata question, if anyone can write it out in other ways.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.