【发布时间】:2013-04-15 21:52:53
【问题描述】:
帮助我有一个需要解决的难题! 我做了一个斐波那契数列,但我忘了包括 0 谁能帮我解开这个谜语? 如果序列中输入为 5,则输出应为 0,1,1,2,3 在不完全从头开始的情况下,我应该更改什么来清理代码并获得所需的结果?
//Class Assignment 9 little Fibonacci series based on what input
//the user provides
import java.util.Scanner;
public class LittleFibonacci {
int number;// This declares an int variable
public static void main(String[] args){
//itnu is the new object
LittleFibonacci itnu = new LittleFibonacci ();
itnu.GetNumberInput();
}
public void GetNumberInput()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a number and that number will be a" +
" \nrepresentitive of the length of a sequence in the Fibonocci series.");
number = input.nextInt();
int f1, f2=0, f3=1;
for(int i = 1 ; i <= number ; i++ )
{
System.out.print(" "+f3+" ");
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
input.close();
}
}
【问题讨论】:
-
在您的
for循环开始之前将您的代码更改为输出0,并将循环迭代次数缩短1(通过从循环终止值中删除一个字符)。 -
注意是斐波那契,不是斐波那契。