簡體   English   中英

變量無法解析,Eclipse中有錯誤,但BlueJ中沒有

[英]Variable cannot be resolved, error in Eclipse but not BlueJ

是的,我知道這個問題有很多重復之處。

以下代碼在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);
    }
}

正如YCF_L提到的char selection:超出范圍。 在Eclipse中工作時,我通常在do while語句之前聲明它。 不要考慮太多;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM