-1
public class FibonacciGenerator
{  
    //instance variables
    private int recent ; //one values ago
    private int previous ; //two values ago
    private int n ; // the number of values returned so far

    /**
       Constructs the generator by setting the instance variables to 1
    */
    public FibonacciGenerator()
    {   
        recent = 1 ;
        previous = 1 ;
        n = 0 ;
    }

    /**
       Produces the next Fibonacci number
       @return the next in the Fibonacci sequence
    */
    public int next()
    {  
        n ++ ;
        if (n == 1) return 1 ;
        if (n == 2) return 1 ;
        int result = recent + previous ;
        //----------------Start below here. To do: approximate lines of code = 3
        // 1. Update previous and recent, and 2. return result.
        previous++;
        recent++;
        return result;
        //----------------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
    }
}

import java.util.* ;

public class FibonacciGeneratorTester
{
    public static void main(String[] args)
    {
        System.out.println("The 1st Fibonacci number is: "
                   + getFibonacci(1)) ;
        System.out.println("The 10th Fibonacci number is: "
                   + getFibonacci(10)) ;
    }
    /**
       A static method to return the n'th Fibonacci number
       @param n the index of the Fibonacci number
       @return the n'th Fibonacci number
     */
    public static int getFibonacci(int n)
    {
        FibonacciGenerator generator = new FibonacciGenerator() ;
        int result = 0 ;
        //----------------Start below here. To do: approximate lines of code = 4
        // 1. Write a for-loop that calls the generator n times 2  . return the last result of the call.
        for (int i = 1; i <= n; i++){
            generator.next();
            return result;
        }
    }
} 

缺少返回语句倒数第二个大括号突出显示。我的 for 循环是否正确?................................................ ..................................................... ..................................................... …………

4

4 回答 4

3

您的 for 循环开始:

for (int i = 1; i <= n; i++){

如果 n 小于 1,它将立即退出,并且在循环结束和方法结束之间没有 return 语句。那就是缺少的 return 语句。

于 2013-11-04T01:26:34.770 回答
1

看这个:

public static int getFibonacci(int n)
{
    FibonacciGenerator generator = new FibonacciGenerator() ;
    int result = 0 ;
    //----------------Start below here. To do: approximate lines of code = 4
    // 1. Write a for-loop that calls the generator n times 2  . return the last result of the call.
    for (int i = 1; i <= n; i++){
        generator.next();
        return result;
    }
}

它必须返回一个 int。仔细看看你的循环。什么是 n = 0?没错,它不会返回任何东西。这是不允许的。

于 2013-11-04T01:27:25.253 回答
1
public static int getFibonacci(int n)
{
    FibonacciGenerator generator = new FibonacciGenerator() ;
    int result = 0 ;
    //----------------Start below here. To do: approximate lines of code = 4
    // 1. Write a for-loop that calls the generator n times 2  . return the last result of the call.
    for (int i = 1; i <= n; i++){
        generator.next();
    }
return result;
//put a return statement here, instead of in your loop. 
}
于 2013-11-04T01:29:40.937 回答
1

试试这个代码:

import java.util.*;   
public class Fibonnacci{
 public static void main(String args[]){
  int num;
  Scanner in=new Scanner(System.in);
  System.out.println("Enter an integer");
  num = in.nextInt();
  System.out.println("Fibonacci Series");
  int sum=0,a=0,b=1;
  for(int i=0;i<num;i++){
   System.out.print(" "+sum);
   a = b;
   b = sum;
   sum = a + b;
  }
 }
}
于 2015-04-12T16:14:45.583 回答