Use Java to implement the Fibonacci sequence (Fibonacci) public class Test {public int f (int n)//n represents the first number. The program returns its corresponding value {return n>2?f (n-1) +f (n-2): 1;//seems so graceful a program}//the programmer's mind: The above code will be refactored. Make them easier to read. Recommended!!! public int Fibo (final int pos) {final int num;if (pos>2) num = Fibo (pos-1) +fibo (pos-2); Elsenum=1;return num;} public static void Main (string[] args) {Test t = new Test (); System.out.println (T.F (8));//Call Program Ape Write method Output 21system.out.println (T.fibo (9));//Call Program Designer to write method output 34//1 2 3 4 5 6 7 8 .... Number//1 1 2 3 5 8 13 21 ... corresponding value}}
The method of the Fibonacci sequence is written in two ways: The program ape Thinking and the program designer's thinking.