簡體   English   中英

為什么我的代碼特別出現3個錯誤?

[英]Why do I get 3 errors with this code in particular?

    if (answer == "help") {
        for (int i = 0; i < enterCommand.length; i++){
            try {
                Thread.sleep(1000);
                System.out.println(help[i]);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }

我得到的錯誤是:

  • 這行有多個標記

    • 語法錯誤,插入“)語句”以完成IfStatement

    • 幫助無法解析為變量

    • 令牌“ if”上的語法錯誤,(此令牌后應為

感謝您的任何建議!

您尚未定義一個名為help的變量。

您沒有使用結束括號結束if塊}

除了這些語法錯誤之外,另一個問題是您正在使用==比較字符串。 應該使用equals方法比較字符串。

   if (answer == "help") 

應該替換為

   if ("help".equals(answer) )

建議 :如果不學習編程的基本結構,請不要參與編碼。 因此,請先閱讀基礎知識,以避免過多的掙扎。

首先,您的代碼還不夠完整,只是猜測游戲無法正確回答。

看起來您正在比較“ if”語句中的兩個two。 代替==使用.equals方法,例如:

if ( answer.equals("help") ) {
    for (int i = 0; i < enterCommand.length; i++){
        try {
            Thread.sleep(1000);
            System.out.println(help[i]);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }

} // end of for
} // end of if

希望這可以解決您的查詢,然后檢查輸出.....否則,請再次發布完整代碼。

暫無
暫無

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

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