package HspLearningoop;
public class Demon09Fibonacci {
public static void main(String[] args) {
T t1 = new T();
int n = 8;
int res = t1.fibonacci(n);
if (res != -1){
System.out.println("当n等于"+ n + "时,斐波那契数为:"+res);
}
}
}
class T {
public int fibonacci(int n) {
if (n >= 1) {
if (n == 1 || n == 2) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
} else {
System.out.println("你输入的数据有误");
return -1;
}
}
网友评论