Java fibonacci - 从数组中查找用户输入

时间:2017-03-22 17:30:19

标签: java fibonacci

我对我的斐波纳契码有几个问题。如果我想在数组(输入)中找到数字5的位置,系统会说它的位置是5,但应该说6?此外,如果我输入的数字不在数组中,系统会在位置20中找到它(示例输入200)。系统应该说它没有找到"

    int totalFibos=20;
    int fibs[] = new int[totalFibos];

    int a=0;
    int b=1;
    int fib=0;
    fibs[0]=0;

    fibs[1]=1;

    int fibCounter=2;
    while(fibCounter<totalFibos)
    {
        fib=a+b;
        a=b;
        b=fib;
        fibs[fibCounter]=fib;
        fibCounter++;
    }
    System.out.println("\n\nThe first "+ totalFibos + " fibonacci numbers are: ");
    int i=0;

    while(i<totalFibos)
    {
        System.out.print(fibs[i]+" ");
        i=i+1;
    }
    System.out.println();
    int userInput=0;
    i=0;
    Scanner sc= new Scanner(System.in);
    int found=0;
    while(userInput!=-1)
    {
        System.out.print("Enter a number to search,(enter -1 to end the search): ");
        userInput= sc.nextInt();

        while (i<totalFibos)
        {
            if(userInput==fibs[i])
            {
                found=1;
                break;
            }
            i++;
        }
        if(found==1)
            System.out.println("The number: " + userInput + " is found at location: "+ i++);
                    else if (found==0)
                            System.out.println("The number: "+ userInput + " is not found");
            }

                    if(userInput==-1)
            System.out.println("\nThanks");
            }



    }

2 个答案:

答案 0 :(得分:3)

请注意,你有&#34 ;;&#34;在

中的if语句后签名
    if(userInput==fibs[i]);
    {
        found=1;
        break;
    }

这就是为什么总是执行{}中的表达式。 删除&#34;;&#34;并添加i ++;在while循环中的某个地方为此工作。

更新: 每次输入后还要重置计数器和结果。这应该是你的while循环中的第一件事:

    found = 0;
    i = 0;

答案 1 :(得分:1)

以下是您重构的代码:

int totalFibos=20;
int fibs[] = new int[totalFibos];

int a=0;
int b=1;
int fib=0;
fibs[0]=0;

fibs[1]=1;

int fibCounter=2;
while(fibCounter<totalFibos)
{
    fib=a+b;
    a=b;
    b=fib;
    fibs[fibCounter]=fib;
    fibCounter++;
}
System.out.println("\n\nThe first "+ totalFibos + " fibonacci numbers are: ");
int i=0;

while(i<totalFibos)
{
    System.out.print(fibs[i]+" ");
    i=i+1;
}
System.out.println();
int userInput=0;
i=0;
Scanner sc= new Scanner(System.in);
int found=0;
while(userInput!=-1)
{
    System.out.println("Enter a number to search,(enter -1 to end the search): "); //Shouldnt this be println ?
    userInput= sc.nextInt();
    i=0; //Reset it, else the second search would fail
    found=0; //Reset it, else the second search would fail
    while (i<totalFibos)
    {
        if(userInput==fibs[i]);
        {
            found=1;
            break;
        }
        i++; //Change state of i

    }
    if (found==1) { //Wheres your bracket ?
       System.out.println("The number: " + userInput + " is found at location: "+ Integer.toString(i+1)); //Just to be sure...
    }
    else if (found==0) {
       System.out.println("The number: "+ userInput + " is not found");
    }
 }
    if(userInput==-1)
        System.out.println("\nThanks");
    }

希望它有效并帮助你!