Java fibonacci序列的一种编写方法
我正在尝试编写一个for循环,它调用fibonacci方法并打印fibonacci序列中的前25个数字。问题是我有点困惑如何正确地做到这一点Java fibonacci序列的一种编写方法,java,Java,我正在尝试编写一个for循环,它调用fibonacci方法并打印fibonacci序列中的前25个数字。问题是我有点困惑如何正确地做到这一点对于run方法中的for循环何时调用fibonacci方法,我有点困惑。在for循环到达pass之后,fibonacci方法中的值是否重置?例如,在for循环i=0的第一个过程中,int a和int b的值在fibonacci方法内部发生变化。斐波那契方法中的值是否在for循环的下一个过程中重置 import acm.program.*; public c
对于run方法中的for循环何时调用fibonacci方法,我有点困惑。在for循环到达pass之后,fibonacci方法中的值是否重置?例如,在for循环i=0的第一个过程中,int a和int b的值在fibonacci方法内部发生变化。斐波那契方法中的值是否在for循环的下一个过程中重置
import acm.program.*;
public class Fibonacci extends ConsoleProgram{
private void run(){
for(int i = 0; i <= 25; i++){
fibonacci(i);
println(fibonacci(i));
}
}
private int fibonacci(int n){
int n = 0;
int a = 0;
int b = 1;
while (n < 25);
int c = a + b;
a = b;
b = c;
}
return(a);
}
导入acm.program.*;
公共类Fibonacci扩展控制台程序{
私家车{
对于(int i=0;i您没有存储返回的fibonacci()的int值
int currentSum=0;
对于(int i=0;i您在两个不同的位置循环-run()
和fibonacci()
。这些位置中只有一个应该关心循环,另一个应该关心计算fibonacci(n)
我们可以做的是从fibonacci
中删除循环,并且只依赖外部的循环。此外,我们还将删除该语句int n=0
,因为它会隐藏您传递的参数
最后,我们将创建两个新的静态变量a
和b
,以便在这个实例中保留这些变量的值。如果不这样做,则必须依赖递归或其他方法来提供a
和b
的适当值
我不完全确定您为什么需要扩展控制台程序
,但现在我将保留它
所以,这里是它应该是什么样子
public class Fibonacci extends ConsoleProgram {
static int a = 0;
static int b = 1;
public void run() {
for(int i = 0; i <= 25; i++) {
// Print the call to fibonacci(i) with every iteration.
}
}
private int fibonacci(int n) {
int c = a + b;
a = b;
b = c;
return c;
}
}
公共类Fibonacci扩展控制台程序{
静态int a=0;
静态int b=1;
公开募捐{
对于(int i=0;iFibonacci),这是一个典型的算法示例,可以很容易地通过递归实现,这是因为:
- 你可以分步分割整个斐波那契序列
- 在每一步中,你必须做同样的事情,除了最后一步得到0
- 最后一步是“特殊的”,因为0乘以任何数字都会得到0
因此,如果您应用与之前相同的步骤,只需将所有内容置零,这意味着当计数器为0时,您必须执行与之前步骤不同的操作,它是:
- 将您存储的结果乘以1而不是0(或者您可以保持原样,这与乘以1是一样的)
- 退出循环并终止斐波那契序列
互联网上到处都是斐波那契的例子,对你来说已经足够了。变量在循环的每次迭代中都会重置。变量a
、b
和c
都是局部变量,它们只在方法中“存在”。对斐波那契(n)的每次调用
方法应该从斐波那契序列的开头开始,打印出直到第n个项的项。因此,while(n<25);
不应该是该方法的一部分。另外,int n=0
将n重置为零,这是不好的,因为我们需要知道n是什么来获得第n个项
执行此循环的理想方法是:
private void fibonacci(int n) {
int i = 1; // a new variable i to count from the 1st term
int a = 0; // the first term
int b = 1; // the second term
while (i <= n) {
int c = a + b; // the new value for b
a = b; // switch the old a for the new a
b = c; // get the (i + 1)th term for the next iteration
System.out.println(a); // print out the ith term
i++;
}
}
private void fibonacci(int n){
int i=1;//从第一项开始计算的新变量i
int a=0;//第一项
int b=1;//第二项
虽然(你是说fibonacci方法顶部的int n=0?那是一个mistake@makote你是说让程序的一部分专注于循环,让程序的另一部分专注于计算斐波那契?我不知道循环在哪里?循环属于run
。本质上,循环的主体只更改为包含System.out.println(斐波那契(n))
。我问题中的for循环是否仍包含在您的答案中?如果是,静态int a=0;静态int b=1;是否包含在for循环中?抱歉,这是一个愚蠢的问题,我只是有点困惑。我将继续进一步澄清我的答案。感谢您的澄清,感谢您花时间与他交流请帮我解决我在这个问题上遇到的问题。
public class Fibonacci extends ConsoleProgram {
static int a = 0;
static int b = 1;
public void run() {
for(int i = 0; i <= 25; i++) {
// Print the call to fibonacci(i) with every iteration.
}
}
private int fibonacci(int n) {
int c = a + b;
a = b;
b = c;
return c;
}
}
private void fibonacci(int n) {
int i = 1; // a new variable i to count from the 1st term
int a = 0; // the first term
int b = 1; // the second term
while (i <= n) {
int c = a + b; // the new value for b
a = b; // switch the old a for the new a
b = c; // get the (i + 1)th term for the next iteration
System.out.println(a); // print out the ith term
i++;
}
}