赞
踩
- public static void main(String[] args) {
- // 方法一;数组
- int num[] = new int[20];
- num[0] = 0;
- num[1] = 1;
- System.out.println("斐波那契数列前20项的值如下:");
- System.out.print(num[0]+" "+num[1]+" ");
- for(int i = 2; i < 20; i++){
- num[i] = num[i - 1] + num[i - 2];
- System.out.print(num[i]+" ");
- }
- System.out.println();
- // 方法二
- int a = 0;
- int b = 1;
- for(int i = 1;i <= 10;i++) {
- //循环打印a,b两个数,即两个两个打印
- System.out.print(a + " " + b + " ");
- //打印第三、四个数
- a = a + b;
- b = a + b;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。