实现Fibonacci数列功能,并进行测试。
- 源代码:
import java.util.Scanner;
public class fibonacci {
public static void main(String[] args) {
System.out.println("how many numbers?");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if(n == 1)
System.out.println("f(0)=0");
if(n == 2)
System.out.println("f(0)=0
f(1)=1");
if(n > 2){
System.out.println("f(0)=0
f(1)=1");
int a = 0;
int b = 1;
int temp = 0;
for(int i=2;i<=n;i++){
System.out.println("f("+i+")="+(a+b));
temp = b;
b = a+b;
a = temp;
}
}
else{
System.out.println("error input");
}
}
}
- 实验截图