2019-09-30
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Demo01 { public static void main(String[] args) { /** * 11~23,得到第n个斐波那契数 */ List<Integer> list = new ArrayList<Integer>(); Scanner sc = new Scanner(System.in); System.out.println("你想获取第几个斐波那契数:"); int n = sc.nextInt(); for (int i = 0; i < n; i++) { list.add(me(i)); } System.out.println("第"+n+"个斐波那契数是:"+list.get(n - 1)); /** * 28~31,得到前10个斐波那契数 *//* for (int i=0;i<10;i++) { System.out.println(me(i)); }*/ } static int me(int n) { if (n < 0) { throw new IllegalArgumentException("x<0"); } if (n == 0) { return 0; } if (n == 1) { return 1; } return me(n - 1) + me(n - 2); } }