在Eclipse中无法解决变量错误 [英] Cannot be resolved to a variable error in Eclipse

查看:147
本文介绍了在Eclipse中无法解决变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编写一个打印斐波纳契序列的简单程序。我想创建一个名为fibNumber的方法来计算斐波那契序列的值,然后我想在run()方法中使用一个for循环来打印该值15次。我遇到的麻烦是for循环中的println方法。 Eclipse说n不能被解析为一个值,我不能被解析为一个值。我以为我在宣布变数方面涵盖了所有的基础。我错过了什么吗?



我想写的是一直到F15的



F0 = 0

F1 = 1

F2 = 1

F3 = 2

F4 = 3

F5 = 5

  import acm.program。*; 


public class FiccononicSequence扩展ConsoleProgram {

public void run(){
println(此程序打印出斐波纳契序列); (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);




}


解决方案

尝试这个...



- 这里的问题是关于



- 应该被声明为 int ,这是 local run()方法代替 n ,因为 n 是另一个本地 fibNumber()方法中的变量



- i n 完全是不同的范围,每个不可见 (int i = 1; i = 15; i ++){

(b)

 println(F+ i +=+ fibNumber(i)); //我应该在这里

}


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?

What I want to write is all the way up to F15

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);




}      

解决方案

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.

- i and n are totally in different scope and are invisible to each other.

for (int i = 1; i <= 15; i++){

        println("F" + i + " = " + fibNumber(i));  // i should be here.

    }

这篇关于在Eclipse中无法解决变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆