Java Fibonnaci序列
作为Java编程的初学者,我正在查找一些Java代码。我发现:Java Fibonnaci序列,java,Java,作为Java编程的初学者,我正在查找一些Java代码。我发现: import java.util.Scanner; class FibonacciSeries { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter the value of the number upto which the sequence should cont
import java.util.Scanner;
class FibonacciSeries {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the value of the number upto which the sequence should continue: ");
int n = s.nextInt();
fibonacci(n);
}
public static void fibonacci(int n) {
if (n == 0) {
System.out.println("0");
} else if (n == 1) {
System.out.println("0 1");
} else {
System.out.print("0 1 ");
int a = 0;
int b = 1;
for (int i = 1; i < n; i++) {
int nextNumber = a + b;
System.out.print(nextNumber + " ");
a = b;
b = nextNumber;
}
}
}
}
import java.util.Scanner;
类斐波那契级数{
公共静态void main(字符串[]args)
{
扫描仪s=新的扫描仪(System.in);
System.out.print(“输入序列应持续到的数字值:”);
int n=s.nextInt();
斐波那契(n);
}
公共静态无效斐波那契(int n){
如果(n==0){
系统输出打印项次(“0”);
}else如果(n==1){
系统输出打印项次(“01”);
}否则{
系统输出打印(“01”);
int a=0;
int b=1;
对于(int i=1;i
谁能告诉我在公开静态无效斐波那契(int n)之后到底发生了什么?一点理论:
if (n == 0) { //if user insert 0 than print to console 0
System.out.println("0");
} else if (n == 1) { //if user insert 1 than print to console 0 1
System.out.println("0 1");
}
else { //for every other cases (2, 3,... x)
System.out.print("0 1 "); //print to console 0 1
int a = 0;
int b = 1; //before cycle prepare two values 0 and 1
for (int i = 1; i < n; i++) { //Cycle since i which starts on 1 passed condition "is lover than inserted value"
int nextNumber = a + b; //store value of 0 + 1
System.out.print(nextNumber + " "); //print this stored value
a = b; //Set value in b into to variable a
b = nextNumber; //set stored value as b
} //Increment i plus 1 and repeat cycle
}
if(n==0){//if用户插入0而不是打印到控制台0
系统输出打印项次(“0”);
}else if(n==1){//如果用户插入1而不是打印到控制台0 1
系统输出打印项次(“01”);
}
else{//对于每一个其他情况(2,3,…x)
System.out.print(“0 1”);//打印到控制台0 1
int a=0;
int b=1;//在循环前准备两个值0和1
对于(int i=1;i
因此,在最后你将有如果用户插入5。打印01并从循环(0+1)1(1+1)2(1+2)3方法的循环结束返回主循环。对于5个打印的0 1 2 3。仅此而已。您在这里看到的是一个实现,最多可实现给定数量的数字
n
,再加上数字0
斐波那契数列中的前两个数是1和1,或0和1,这取决于所选的数列起点,随后的每一个数都是前两个数的总和
根据定义,这些数字序列在算法中以0
和1
开头
这两条语句处理用户输入
0
或1
作为限制,并分别打印0
或01
的可能性。(这就是System.out.println();
的作用)
第三种情况(如果输入既不是0
也不是1
)计算更长的序列:
else {
System.out.print("0 1 "); //this statement prints the start "0 1 "
int a = 0; //the two starting numbers a and b are initialized
int b = 1;
for (int i = 1; i < n; i++) { //for the given amount of numbers:
int nextNumber = a + b; //calculate the next number as sum of both previous numbers
System.out.print(nextNumber + " "); //print this number
a = b; //set the old b as new a
b = nextNumber; //set the new number as b
} //repeat
}
让我们看看在计算样本时,值将如何随每次迭代而变化:
╔═══╦═══╦═══╦════════════╦═══════╦═══════╗
║ i ║ a ║ b ║ nextNumber ║ new a ║ new b ║
╠═══╬═══╬═══╬════════════╬═══════╬═══════╣
║ 1 ║ 0 ║ 1 ║ 1 ║ 1 ║ 1 ║
║ 2 ║ 1 ║ 1 ║ 2 ║ 1 ║ 2 ║
║ 3 ║ 1 ║ 2 ║ 3 ║ 2 ║ 3 ║
║ 4 ║ 2 ║ 3 ║ 5 ║ 3 ║ 5 ║
║ 5 ║ 3 ║ 5 ║ 8 ║ 5 ║ 8 ║
╚═══╩═══╩═══╩════════════╩═══════╩═══════╝
这是定义斐波那契序列的方法。我想这更清楚
public static void fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n-1) + fibonacci(n-2);
}
}
您能否告诉我们您目前的理解,以帮助我们确定您的困境?您是否了解如何生成斐波那契数列(即,您是否可以使用笔和纸)?如果没有,就从那里开始。如果你这样做了,请看Sheepy的评论。@OlivierGrégoire,你是对的。你抓住我了。我必须承认,我读了足够多的代码,看到了我解释为停止条件的内容,并假设“初学者”会被递归搞糊涂。在你指出之前,我从未读过这个循环。谢谢你的更正。
╔═══╦═══╦═══╦════════════╦═══════╦═══════╗
║ i ║ a ║ b ║ nextNumber ║ new a ║ new b ║
╠═══╬═══╬═══╬════════════╬═══════╬═══════╣
║ 1 ║ 0 ║ 1 ║ 1 ║ 1 ║ 1 ║
║ 2 ║ 1 ║ 1 ║ 2 ║ 1 ║ 2 ║
║ 3 ║ 1 ║ 2 ║ 3 ║ 2 ║ 3 ║
║ 4 ║ 2 ║ 3 ║ 5 ║ 3 ║ 5 ║
║ 5 ║ 3 ║ 5 ║ 8 ║ 5 ║ 8 ║
╚═══╩═══╩═══╩════════════╩═══════╩═══════╝
public static void fibonacci(int n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n-1) + fibonacci(n-2);
}
}