作为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 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;
}
}
}
}
有人可以告诉我public static void fibonacci(int n)
后到底发生了什么事吗?
答案 0 :(得分:1)
理论上的一点点: Fibonacci number
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
}
所以最后你会有用户插入5.打印 0 1 和周期(0 + 1) 1 (1 +1) 2 (1 + 2) 3 方法循环结束时返回main。 5印刷0 1 1 2 3.就是这样。
答案 1 :(得分:1)
定义斐波那契序列的另一种方法。我认为它更清楚。
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);
}
}
答案 2 :(得分:0)
您在此处看到的是algorithm that calculates fibonacci numbers的实施,最多为一定数量的数字n
,加上数字0
。
Fibonacci序列中的前两个数字是1和1,或0和1,具体取决于所选择的序列起点,后面的每个数字都是前两个数字的总和。
根据定义,这些数字序列以算法中的0
和1
开头。
if (n == 0) {
System.out.println("0");
} else if (n == 1) {
System.out.println("0 1");
}
这两个语句处理了用户输入0
或1
作为限制的可能性,并分别打印0
或0 1
。 (那是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
}
因此,对于6
的示例输入,输入else
个案,打印
0 1
之后,for-each循环将运行5次,打印:
1
,2
,3
,5
,8
这是第一个n
+ 1个斐波纳契数。
对for
语句如何工作有一个阅读here,以便更好地了解最新情况,但基本上它说:
for("start here"; "repeat as long as this condition is true"; "after each step do this")
{
//Everything between the brackets will be executed once for every run through the loop.
}
让我们看看在计算样本时每次迭代时值的变化:
╔═══╦═══╦═══╦════════════╦═══════╦═══════╗
║ 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 ║
╚═══╩═══╩═══╩════════════╩═══════╩═══════╝