Java Fibonacci 系列 - 数组中的返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15855095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:03:43  来源:igfitidea点击:

Java Fibonacci Series - Return Values in Array

javaarraysrecursionfibonacci

提问by corneria

I'm trying to recursively compute the fibonacci sequence to 100, store those returned values into an array using a the buildArray method, then print values stored in the array. I am getting a "cannot be resolved to a variable" compilation error when I try to print A[N] in the main method. I'm using longs because I'm computing the series up to 100, although I don't know if it's necessary to use longs.

我正在尝试将斐波那契数列递归计算为 100,使用 buildArray 方法将这些返回值存储到数组中,然后打印存储在数组中的值。当我尝试在 main 方法中打印 A[N] 时,出现“无法解析为变量”编译错误。我使用 longs 是因为我计算的序列最多为 100,虽然我不知道是否有必要使用 longs。

If I substitute F(N) for A[N] the code works, but I need to put the values into an array and print that array. Does this code even store the values in an array? I'm just starting java, thanks.

如果我用 F(N) 代替 A[N] 代码有效,但我需要将值放入数组并打印该数组。这段代码甚至将值存储在数组中吗?我刚刚开始java,谢谢。

public class MyFibonacci {
    public static final int MAX = 100;

    public static long[] buildArray(int MAX, int N) {
        long[] A = new long[MAX];

        A[0] = 0;
        A[1] = 1;

        for(N = 2; N < MAX; N++)
            A[N] = F(N);
        return A;
    }

    public static long F(int N) {
        if(N == 0)
            return 0;
        if(N == 1)
            return 1;
        return F(N - 1) + F(N - 2);
    }

    public static void main(String[] args) {
        for(int N = 0; N < MAX; N++)
            System.out.println(N + " " + A[N]);
    }
}

采纳答案by Bernhard Barker

The main problem is that you're never calling the buildArrayfunction.

主要问题是您从不调用该buildArray函数。

To get your code to work, you only need to add this to main:

要使您的代码正常工作,您只需要将其添加到main

long[] A = buildArray(MAX, 0);

Some other things:

其他一些事情:

You can remove the parameter Nand just declare it in the function (or remove it all-together, see below).

您可以删除参数N并在函数中声明它(或一起删除它,见下文)。

You already have access to MAX, no need to pass it to the function.

您已经可以访问MAX,无需将其传递给函数。

The for-loop in buildArrayis rather inefficient, you can set up the array inside F.

in 的 for 循环buildArray效率很低,可以在里面设置数组F

Given the below, Aas a class variable is cleaner than passing it around.

鉴于以下内容,A作为类变量比传递它更干净。

Finally, the code:

最后,代码:

static int MAX = 100;
static long[] A;
public static void buildArray()
{
  A = new long[MAX+1];
  F(MAX);
}

public static long F(int N)
{
  long val;
  if (N < 2)
    val = N;
  else if (A[N] != 0) // HEY! It's already calculated! Awesome! Just return it.
    return A[N];
  else
    val = F(N-1) + F(N-2);
  A[N] = val;
  return val;
}
public static void main(String[] args)
{
  buildArray();
  for (int N = 0; N <= MAX; N++)
    System.out.println(N + " " + A[N]);
}

回答by corneria

You have declared A[]within the scope of buildArray(int MAX, int N). As a result, A[]is not accessible outside of buildArray. You need to move your declaraction of long A[]to a class variable.

您已A[]在 范围内声明buildArray(int MAX, int N)。因此,A[]buildArray. 您需要将您的声明移动long A[]到类变量。

Additionally, you actually need to run buildArrayfor the array to be constructed.

此外,您实际上需要buildArray为要构造的数组运行。

For future reference, I highly recommend using proper tabbing structures. It makes it much easier to see what's happening. I've edited your code (though it will have to be approved) to include this.

为了将来参考,我强烈建议使用适当的制表结构。它使查看正在发生的事情变得更加容易。我已经编辑了您的代码(尽管它必须获得批准)以包含此内容。

回答by d'alar'cop

Here's the code for what you need, I think:

这是您需要的代码,我认为:

public class MyFibonacci{

    public static final int MAX = 100;
    long[] A = new long[MAX];
    public static long[] buildArray(int N){
    A[0] = 0;
    A[1] = 1;

    for (N = 2; N < MAX; N++){
        A[N] = F(N);
        }
    return A;
    }

    public static long F(int N)
    {
   if (N == 0) return 0;
   if (N == 1) return 1;
   return F(N-1) + F(N-2);
    }


    public static void main(String[] args)
    {
    buildArray(<some number - not sure where you get it from? N by the way in buildArray()>);
   for (int N = 0; N < MAX; N++)
      StdOut.println(N + " " + A[N]);
    }
 }

回答by Andrej

Since you can allocate array memory, it makes good sense to utilize it during calculation. Consider this method:

由于您可以分配数组内存,因此在计算过程中使用它很有意义。考虑这个方法:

public static long[] f_a(int n) {
    long[] a = new long[n];
    a[1] = 1;
    for (int i = 2; i < n; i++)
        a[i] = a[i-1] + a[i-2];
    return a;
}