一、第一种
public static void feibio(int n){
int[] arrayist = new int[n];
arrayist[0]=arrayist[1]= 1;
for(int i = 0;i<=arrayist.length;i++) {
if(i==0) {
System.out.println("第"+(i+1)+"等于"+arrayist[0]);
}else if(i==1) {
System.out.println("第"+(i+1)+"等于"+arrayist[1]);
}else {
arrayist[i]=arrayist[i-1]+arrayist[i-2];
System.out.println("第"+(i+1)+"等于"+arrayist[i]);
}
}
}
第二种方法
public static void feibo2(int n) {
int[] arrayList = new int[n];
arrayList[0] = arrayList[1] =1;
for (int i = 0; i < arrayList.length; i++) {
if (i == 0) {
System.out.println("第" + (i+1) + "等于" + arrayList[0]);
}else if (i == 1) {
System.out.println("第" + (i+1) + "等于" + arrayList[1]);
}else {
arrayList[i] = arrayList[i-1] +arrayList[i-2];
System.out.println("第" + (i+1) + "等于" + arrayList[i]);
}
}
}
第三种
public static int feibo3(int n) {
if (n == 1 || n == 2) {
return 1;
}else {
return feibo3(n-1) + feibo3(n-2);
}
}
public static void feibo4(int n) {
for(int i = 1;i<=n;i++) {
int m = feibo3(i);
System.out.println("第"+i+"等于"+ m);
}
}