Java Program Fibonacci Sequence

前端 未结 15 1872
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 03:17

I am writing a \"simple\" program to determine the Nth number in the Fibonacci sequence. Ex: the 7th number in the sequence is: 13. I have finished writing the program, it

15条回答
  •  星月不相逢
    2020-12-17 03:27

    It looks better with multiple statements of ternary operator.

    static int fib(int n) {
            return  n > 5 ? fib(n-2) + fib(n-1)
                          : n < 2 || n == 5 ? n
                                            : n - 1;
    }
    

提交回复
热议问题