//裴波那契数列的定义:他的第一项和第二项均为1,以后各项都为前两项的和。如:
1,1,2,3,5,8,13,21,34,55,89,144,233,~~~~
关键代码如下:
package fuxi;
import java.util.Scanner;
public class Pbo {
public static void main(String[] args) {
//获取客户输入参数。
Scanner s = new Scanner(System.in);
System.out.println("请输入您要查看的裴波那契数列:");
int num = s.nextInt();
System.out.println("您想看的裴波那契数列是:");
f(num/2+1);
}
//打印数列的方法
private static void f(int x) {
int f1=1;
int f2=1;
int i=3;
if (x==1) System.out.print(f1);
if (x==2) System.out.print(f1+" "+f2);
if (x>=3){
System.out.print(f1+" "+f2);
while (x>=i) {
f1=f2+f1;
System.out.print(" "+f1);
i++;
f2=f2+f1;
System.out.print(" "+f2);
}
}
}
}