繁体   English   中英

斐波那契数列返回参数

[英]Fibonacci Sequence return argument

我需要生成一个生成斐波那契数列的程序,这是我到目前为止所拥有的:

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());

说:类EP64中的方法nextNumber不能应用于给定类型:必需:int发现:无参数原因:实际参数和形式参数列表的长度不同

有人可以告诉我是否正在执行此程序吗? 如果没有,请帮助! 我看了其他类似的问题,但我对这些问题没有多大理解

谢谢你们!

无法将类EP64中的方法nextNumber应用于给定类型:必需:发现的整数:无参数原因:实际参数和形式参数列表的长度不同

你的

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 nextNumber

继续阅读

您正在向对象引用而不是类本身调用静态方法。

完全不传递nextNumber()方法的任何参数。

使方法成为非静态方法为:

public void nextNumber(int n) {}

将arg传递给方法为:

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

并且也不要忘记从您在System.out.println中收集的nextNumber方法返回处理后的数字。

您对nextNumber声明说它需要一个int参数,但是您没有参数就调用它。

另外,您的代码将无法执行您想要的操作。 您可能应该使类EP64的fold1fold2成员,并使该方法成为实例方法,而不是静态方法。 您还需要执行fold2 = fold1; 在更新fold1之前。

最后,您需要声明nextNumber返回一个int值,然后实际上让它返回一个int值。

你有两个问题。 首先,您的方法不返回任何内容,即为void 您需要将其设置为int并添加一个return fnew; 在末尾。 另一个问题是您每次都是从头开始,每次都会返回2 您需要通过将它们移动到nextNumber行上方来制作fold1fold2字段。 哦,删除int n参数,因为它没有任何作用。

我同意其他帖子的诊断,但不建议使用成员变量,而建议使用重命名和局部变量。

您可以通过拨打5次电话来索要第5个斐波那契数

fib.next (); 

或一个电话

fib (5);

由于斐波那契数列增长非常迅速,因此在达到溢出边界之前,您几乎没有调用(54)。 因此,如果您重复重新计算相同的序列以打印序列,这不是什么大问题。 递归解决方案会很好。

顺便说一句:EP64是一个非常糟糕的名字。

我认为这就足够了:

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

您可以在类之外调用此方法。

// 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();
        }
    }
|

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM