初学者,学Java,希望把学到的罗列出来,如有问题,希望不吝赐教!多谢!
package com.atguigu.exer2;
import java.util.Scanner;
public class AfterTest1 {
public static void main(String[] args) {
AfterTest1 test = new AfterTest1();
System.out.print("请输入你想要的斐波那契数列的值:");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
System.out.print("斐波那契数列:");
for(int i = 1 ; i <= num; i++){
int num1 = test.f2(i);
System.out.print(num1 + " ");
}
}
public int f2(int n){
if(n == 1 ){
return 1;
}else if(n == 2){
return 1;
}else if(n == 3){
return 2;
}else{
return f2(n - 1) + f2(n - 2);
}
}
}