- public class Test1 {
- public static void main(String[] args) {
- int n;
- int m;
- Scanner scanner=new Scanner(System.in);
- System.err.println("请输入小于30的整数:");
- n=scanner.nextInt();
- m=fib(n);
- System.out.println("斐波那契数列第" + n + "项为:" + m);
- }
- public static int fib(int n){
- int a = 0;
- int b = 1;
- for(int c = 1;c < n;c ++)
- {
- a = a + b;
- b = a - b;
- }
- return a;
- }
-
- }
复制代码
要求输出第n项数,结果输出第n-1项的数,问题出在哪里?
|
|