2013-11-04 96 views
-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循環是否正確?............................................ .................................................. .................................................. .......缺少return return語句Fibonnacci Java

+0

你'previous'和'recent'將永遠是相同的,因爲它們都開始於一個都是由一個總是在同一時間遞增。這已經把你想要的結果搞砸了。 –

回答

3

您的循環開始了:

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

如果n小於1,它會立即退出,且有循環的結束和結束之間沒有return語句的方法。這是缺少的回報聲明。

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? 這是正確的,它不會返回任何東西。這是不允許的。

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