Java fibonacci - 从数组中查找用户输入
Java fibonacci - find user input from array
我的斐波那契代码遇到了一些问题。如果我想在数组(输入)中找到数字 5 的位置,系统说它的位置是 5,但它应该说 6?此外,如果我输入一个不在数组中的数字,系统会说它位于位置 20(例如输入 200)。系统应该说是 "not found"
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");
}
}
注意你有“;”在
中的 if 语句后签名
if(userInput==fibs[i]);
{
found=1;
break;
}
这就是它在 {} 中的表达式总是被执行的原因。
去除那个 ”;”并添加 i++; while 循环内的某处使其工作。
更新:
每次输入后还要重置计数器和结果。这应该是 while 循环中的第一件事:
found = 0;
i = 0;
这是您的重构代码:
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");
}
希望它有用并能帮到你!
我的斐波那契代码遇到了一些问题。如果我想在数组(输入)中找到数字 5 的位置,系统说它的位置是 5,但它应该说 6?此外,如果我输入一个不在数组中的数字,系统会说它位于位置 20(例如输入 200)。系统应该说是 "not found"
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");
}
}
注意你有“;”在
中的 if 语句后签名 if(userInput==fibs[i]);
{
found=1;
break;
}
这就是它在 {} 中的表达式总是被执行的原因。 去除那个 ”;”并添加 i++; while 循环内的某处使其工作。
更新: 每次输入后还要重置计数器和结果。这应该是 while 循环中的第一件事:
found = 0;
i = 0;
这是您的重构代码:
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");
}
希望它有用并能帮到你!