我已经生成了一个程序来计算斐波那契数列,其限制由用户通过键盘给出,但是我遇到了一个问题,因为我已经要求该数列从 1 开始,而不是像我所做的那样从 0 开始。我怎样才能做到这一点?我的斐波那契数列显示我从 1 而不是从 0?
int maxNumber = 0;
int previousNumber = 0;
int nextNumber = 1;
System.out.println("How many numbers you want in Fibonacci:");
Scanner scanner = new Scanner(System.in);
maxNumber = scanner.nextInt();
System.out.print("Fibonacci Series of "+maxNumber+" numbers:");
for (int i = 1; i <= maxNumber; ++i)
{
System.out.print(previousNumber+" ");
int sum = previousNumber + nextNumber;
previousNumber = nextNumber;
nextNumber = sum;
}
BIG阳
猛跑小猪
哈士奇WWW
相关分类