这个程序应该从用户那里得到一个斐波那契数,程序将计算它是什么,同时确保用户输入一个正数和一个不小于斐波那契数 70 的数字。所以,如果用户输入 7,它应该打印 13。 fibcalc() 方法应该进行计算。当我尝试编译程序时,我收到错误“斐波那契类中的方法 fibcalc 不能应用于给定类型:System.out.printf("Fibonacci #%d is %f", num, fibcalc(num, x3));
和”找不到符号“return x3;
这是我的代码:
import java.util.Scanner;
public class Fibonacci
{
public static void main ( String args[] )
{
Scanner input = new Scanner ( System.in );
int num;
double x3 = 0;
System.out.print("Which Fibonacci number would you like? ");
num = input.nextInt();
do
{
System.out.print("Which Fibonacci number would you like? ");
num = input.nextInt();
}while(num >= 0 && num <= 70);
System.out.printf("Fibonacci #%d is %f", num, fibcalc(num, x3));
}
public static double fibcalc(int num)
{
int x1 = 0;
int x2 = 1;
if (num == 0)
return 0;
else if (num == 1)
return 1;
else
for (int x3 = 0; x3 < num; x3++)
{
x3 = x1 + x2;
x1 = x2;
x2 = x3;
}
return x3;
}
}
可能还有其他我错过的问题。我对java很陌生。提前致谢。