2013-01-23 45 views
1

我编码在Eclipse斐波纳契数列,这是我的代码 -Fibonacci序列错误

public class FibonacciAlgorithm { 
    private int a = 0; 
    private int b = 1; 

    public FibonacciAlgorithm() { 
    } 

    public int increment() { 
     int temp = b; 
     b = a + b; 
     a = temp; 
     return value; 
    } 

    public int getValue() { 
     return b; 
    } 
} 

它显示在return value;行说value cannot be resolved to a variable错误。我没有看到任何其他错误。

回答

0

value在哪里定义?你返回一些没有在任何地方定义的东西。

+0

我现在看到了,我会将'value'定义为什么? – Hartja

+0

你可能想要返回b。 –

0

你的方法返回一个int变量,所以你必须定义并返回value为int

0

你没有“价值”定义的,这是你的错误。我不记得那件事,但我认为你不需要a和b,我在我的代码档案中找到了它,希望它有帮助。

public class Fibonacci 
{ 
    public static long fibo(int n) 
    { 
     if (n <= 1) return n; 
     else return fibo(n - 1) + fibo(n - 2); 
    } 

    public static void main() { 
     int count = 5; // change accordingly, bind to input etc. 
     int N = Integer.parseInt(count); 
     for (int i = 1; i <= N; i++) 
      System.out.println(i + ": " + fibo(i)); 
     } 
} 

如果您想保留自己的代码,请尝试返回“b”作为值。

0

我不确定你想要做什么。 如果你有“getValue”方法,我认为“增量”方法应该是无效的。 当你想要当前斐波那契值使用“getValue”方法。

public class FibonacciAlgorithm { 

     private int a = 0; 
     private int b = 1;  

     public FibonacciAlgorithm() { 

     } 

     public void increment() { 
      int temp = b; 
      b = a + b; 
      a = temp; 
     } 

     public int getValue() { 
      return b; 
     }