缺少返回声明Fibonacci Java

时间:2013-11-04 01:23:55

标签: java

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;
        }
    }
} 

缺少return语句第二个大括号突出显示。我的循环是否正确?............................................ .................................................. .................................................. .......

4 个答案:

答案 0 :(得分:3)

你的for循环开始了:

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

如果n小于1,它将立即退出,并且在循环结束和方法结束之间没有返回语句。这是缺少的退货声明。

答案 1 :(得分: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? 这是对的,它不会返回任何东西。这是不允许的。

答案 2 :(得分: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. 
}

答案 3 :(得分: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;
  }
 }
}