Trying to write a simple program that prints the Fibonacci sequence. I want to create a method named fibNumber that calculates the value of the Fibonacci sequence and then I want to use a for loop in the run() method to print that value 15 times. The trouble I'm having is the println method in the for loop. Eclipse says "n cannot be resolved to a value" and "i cannot be resolved to a value." I thought I covered all the bases in terms of declaring the variables. Am I missing something?
尝试编写一个打印斐波那契数列的简单程序。我想创建一个名为fibNumber的方法来计算斐波那契数列的值,然后我想在run()方法中使用for循环来打印该值15次。我遇到的麻烦是for循环中的println方法。Eclipse说“n不能解析为值”和“我不能解析为值”。我想我已经涵盖了所有的基础来声明变量。我遗漏了什么东西?
What I want to write is all the way up to F15
我想写的是一直到F15
F0 = 0
F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F0 = 0 F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5
import acm.program.*;
public class FiccononicSequence extends ConsoleProgram {
public void run(){
println("This program prints out the Fibonacci sequence.");
for (i = 1; i <= 15; i++){
println("F" + i + " = " + fibNumber(n));
}
}
private int fibNumber(int n){
if (n == 0){
return 0;
}else{ if (n == 1){
return 1;
}else{
return fibNumber(n - 1) + fibNumber(n - 2);
}
4 个解决方案
#1
2
Try this...
试试这个…
- The problem here is about the scope
of the variable.
-这里的问题是关于变量的范围。
- i
should be declared of type int
, which is local to the run()
method instead of n
, as n
is another local variable in fibNumber()
method.
-我应该声明为int类型,它是run()方法的局部变量,而不是n,因为n是fibNumber()方法中的另一个局部变量。
- i
and n
are totally in different scope and are invisible to each other.
- i和n完全处于不同的范围内,彼此不可见。
for (int i = 1; i <= 15; i++){
println("F" + i + " = " + fibNumber(i)); // i should be here.
}
#2
0
Whats "n"? You should probably be using "i" instead of "n" there.
“n”什么?你应该用“i”而不是“n”。
#3
0
The problem is how you call the fibnumber
method, because the n
variable isn't declared anywhere in the run
method context:
问题是如何调用fibnumber方法,因为在运行方法上下文中没有声明n变量:
for (int i = 1; i <= 15; i++){
println("F" + i + " = " + fibNumber(n)); //what's n?
}
To fix it, just send the i
variable:
要修复它,只需发送i变量:
for (int i = 1; i <= 15; i++){
println("F" + i + " = " + fibNumber(i)); //now it compiles!
}
#4
0
You need to define i
in the for loop and pass it to fibNumber
您需要在for循环中定义i并将其传递给fibNumber
for (int i = 1; i <= 15; i++){<-- Define i
println("F" + i + " = " + fibNumber(i));<-- pass `i `
}
#1
2
Try this...
试试这个…
- The problem here is about the scope
of the variable.
-这里的问题是关于变量的范围。
- i
should be declared of type int
, which is local to the run()
method instead of n
, as n
is another local variable in fibNumber()
method.
-我应该声明为int类型,它是run()方法的局部变量,而不是n,因为n是fibNumber()方法中的另一个局部变量。
- i
and n
are totally in different scope and are invisible to each other.
- i和n完全处于不同的范围内,彼此不可见。
for (int i = 1; i <= 15; i++){
println("F" + i + " = " + fibNumber(i)); // i should be here.
}
#2
0
Whats "n"? You should probably be using "i" instead of "n" there.
“n”什么?你应该用“i”而不是“n”。
#3
0
The problem is how you call the fibnumber
method, because the n
variable isn't declared anywhere in the run
method context:
问题是如何调用fibnumber方法,因为在运行方法上下文中没有声明n变量:
for (int i = 1; i <= 15; i++){
println("F" + i + " = " + fibNumber(n)); //what's n?
}
To fix it, just send the i
variable:
要修复它,只需发送i变量:
for (int i = 1; i <= 15; i++){
println("F" + i + " = " + fibNumber(i)); //now it compiles!
}
#4
0
You need to define i
in the for loop and pass it to fibNumber
您需要在for循环中定义i并将其传递给fibNumber
for (int i = 1; i <= 15; i++){<-- Define i
println("F" + i + " = " + fibNumber(i));<-- pass `i `
}