【问题标题】:Variable cannot be resolved, error in Eclipse but not BlueJ变量无法解析,Eclipse 中有错误,但 BlueJ 没有
【发布时间】:2018-10-23 04:52:17
【问题描述】:

是的,我知道这个问题有很多重复项。

以下代码在 BlueJ 中编译没有问题,但是,当在 Eclipse 中运行时,我得到以下行的错误:

while (selection != 'Q' && selection != 'q');

错误是:selection 无法解析为变量。

为什么我在 Eclipse 而不是 BlueJ 中遇到此错误?

public class menuMain {

    public menuMain(String args) {

        System.out.println("Welcome to the Project");
        Scanner in = new Scanner(System.in);
        do {
            showMenu();
            String menu = in.nextLine(); // read a line of input
            char selection;

            if (menu.length() > 0) {
                selection = menu.toLowerCase().charAt(0); // extract the first char of the line read
            }
            else  {
                System.out.println("invalid input:\t"+selection);
                System.out.println("Press enter to continue...");
                Scanner itScan = new Scanner(System.in);
                String nextIt = itScan.nextLine();
            } 
            switch (selection)
            {
                case 'a':
                changeTime time = new changeTime ();
                break;

                case 'b':
                watchTime timeStop = new watchTime();
                break;

                case 'q':
                System.out.println("\nEnding Now\n");
                System.exit(0);
                break;

                default: 
                System.out.println("Instruction is invalid");
            }
        } 
        while (selection != 'Q' && selection != 'q');    
        System.exit(0);
    }
}

【问题讨论】:

  • selection不在while部分能理解的范围内,这就是为什么,在do while循环外声明就行了
  • 如果 BlueJ 没有将此称为错误,则 BlueJ 编译器不正确。
  • 不知道@StephenC。在 Eclipse 中打开 BlueJ 文件还有一大堆其他问题。我缺乏经验可能是主要原因。

标签: java eclipse bluej


【解决方案1】:

正如 YCF_L 提到的那样,char selection: 超出了范围。在 Eclipse 中工作时,我通常在 do while 语句之前声明它。不要想太多;-)

【讨论】:

  • 谢谢亚历克斯。我肯定在这上面浪费了太多时间。
猜你喜欢
  • 2014-08-09
  • 2013-05-14
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-12
相关资源
最近更新 更多