2011-11-14 60 views
1

我需要生成產生斐波那契序列 這裏的節目是我到目前爲止有:Fibonacci序列返回參數

import java.util.Scanner; 

public class FibonacciRunner 
{ 
    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 

     System.out.println("Enter n:"); 
     int n = in.nextInt(); 

     EP64 fg = new EP64(); 

     for (int i = 1; i <= n; i++) 
      System.out.println(fg.nextNumber()); 
    } 
} 



public class EP64 
{ 

    public static void nextNumber(int n) 
    { 
     int fold1 = 1; 
     int fold2 = 1; 
     int fnew = fold1 + fold2;   
     fold1 = fnew; 
    } 
} 

我得到一個錯誤:

System.out.println(fg.nextNumber()); 

說: 方法next EP64類中的數不能應用於給定類型: 必需:int 找到:無參數 原因:實際和形式參數列表的長度不同

有人也可以告訴我,如果我正在做這個程序嗎?如果沒有,請幫忙!我看了其他類似的問題,但我不能很好地理解它們

謝謝大家!

+2

你需要花更多的時間學習語言。你的'nextNumber'方法不會返回任何東西,但需要一個參數。你把它稱爲好像它返回了一些東西,而不是給它一個參數。 – Mat

回答

3

方法nextNumber類EP64不能應用於給定類型:要求:誠信部發現:沒有參數的原因:實際的和正式的參數列表長度

public static void nextNumber(int n) 
          ^^^^^^^ 
不同

表示對該方法的任何調用都必須提供一個整數作爲參數。但在這裏:

System.out.println(fg.nextNumber()); 
           ^^ you need to add an integer argument 

你違反了這個提供沒有參數。

由於您的代碼現在讀取,我可能會刪除int n參數。

有人可以告訴我,如果我正在做這個程序嗎?

納阿,不是真的...

  • fold1fold2大概應該是成員變量(所以他們沒有得到每次調用該方法復位),
  • 你」重新忘記更新fold2(您只更新fold1),
  • 另外,您可能想要從nextNumber方法返回int

閱讀上

+0

只要在每次調用時重新初始化,忘記更新'fold2'並不重要;) –

+0

正確。如果他考慮到我的第一個建議,這很重要。 – aioobe

1

你的nextNumber聲明說,它帶一個int參數,但你不帶任何參數調用它。

此外,你的代碼不會做你想做的。您可能應該使EP64類的成員fold1fold2成爲方法而不是靜態方法。在更新fold1之前,您還需要執行fold2 = fold1;

最後,你需要聲明nextNumber返回一個int值,然後實際返回一個int值。

2

您正在調用對象引用的靜態方法,而不是類本身。

而且

若沒有通過任何參數爲nextNumber()方法。

製作方法非靜態爲:

public void nextNumber(int n) {} 

通行證ARG的方法爲:

for (int i = 1; i <= n; i++) 
     System.out.println(fg.nextNumber(n)); 

而且也不要忘記從你nextNumber方法,返回處理其數量你在System.out.println中搜集。

1

你有兩個問題。首先,你的方法不會返回任何東西,即它是void。您需要製作int並在最後添加return fnew;。另一個問題是你每次都從零開始,每次都會返回2。您需要將fold1fold2字段移動到nextNumber以上。哦,並放棄int n的論點,因爲它什麼都不做。

1

我同意其他帖子的診斷,但不建議使用成員變量,而是重命名和局部變量。

你可以要求用5調用5斐波那契數,數到

fib.next(); 

或一個調用

fib (5); 

由於非常迅速的斐波那契序列的增加,你有很少在碰到溢出邊界之前調用(54)。因此,如果您反覆重新計算相同的序列,打印序列,這不是一個大問題。遞歸解決方案將會很好。

Btw .: EP64是一個非常糟糕的名字。

1

我覺得這就夠了:

import java.util.Scanner; 
public class Fibnocci 
{  
public static void main(String []abc) 
{ 
    int a=0,b=1,c; 
    Scanner in=new Scanner(System.in); 
    System.out.print("Enter the Range: "); 
    int n= in.nextInt(); 
    System.out.print(a+" "+b); 
    for(int i=0;i<n-2;i++) //n-2 because we are showing 0,1 initially. 
    { 
     c=a+b; 
     System.out.print(" "+c); 
     a=b; 
     b=c; 
    } 
} 
} 

如果要調用此作爲再方法:

import java.util.Scanner; 
public class Fibnocci 
{  
public static void main(String []abc) 
{   
    Scanner in=new Scanner(System.in); 
    System.out.print("Enter the Range: "); 
    int n= in.nextInt(); 
    callFibonocci(n); 
} 
public static void callFibonocci(int n) 
{ 
    int a=0,b=1,c; 
    System.out.print(a+" "+b); 
    for(int i=0;i<n-2;i++) //n-2 because we are showing 0,1 initially. 
    { 
     c=a+b; 
     System.out.print(" "+c); 
     a=b; 
     b=c; 
    } 
} 
} 

你可以調用這個方法了之類的;

0

// Fibnocci使用C#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace CodeProject 
{ 
    class FibnocciSeries 
    {   
     public int[] FibonacciArray(int length) 
     { 
      int[] fseries = new int[length]; 

      fseries[0] = 0; 
      fseries[1] = 1; 

      if (length == 0) 
       return null; 

      //Iterating through the loup to add adjacent numbers and create the memeber of series   
      for (int i = 2; i < length; i++) 
      { 
       fseries[i] = fseries[i - 1] + fseries[i - 2]; 
      } 
      return fseries; 
     } 

    } 
} 

//////////////////// 

class Program 
    { 
     static void Main(string[] args) 
     { 

      FibnocciSeries fb = new FibnocciSeries(); 
      Console.WriteLine("Please Enter Integer Length of Fibnocci series"); 
      int length = Convert.ToInt32(Console.ReadLine()); 
      int[] result = fb.FibonacciArray(length); 

      foreach(int i in result) 
       Console.Write(i.ToString()+ " "); 
      Console.ReadLine(); 
     } 
    } 
|