2016-01-12 97 views
-4

我正在爲我的APCS課程寫一篇聊天機器人。我得到一個錯誤,錯誤:類,接口或枚舉的預期,我發現通常會引起括號或括號不正確匹配。我有輕微的閱讀障礙,這使得試圖找到這些地獄。如果有人會幫助我,我會非常感激。這是代碼。 公共類Magpie2 {有人可以幫我找到括號/括號是錯的嗎?

public String getGreeting(); 
{ 
    return "Hello, let's talk."; 
} 

/** 
* Gives a response to a user statement 
* 
* @param statement 
*   the user statement 
* @return a response based on the rules given 
*/ 
public String getResponse(String statement) 
{ 
    String response = ""; 
if (statement.length() == 0); 
{ 
    response = "Say something, please."; 
} 
    String response = ""; 
    if (statement.indexOf(" no ") >= 0) 
    { 
     response = "Why so negative?"; 
    } 
    else if (statement.indexOf("mother") >= 0 
      || statement.indexOf("father") >= 0 
      || statement.indexOf("sister") >= 0) 
    { 
     response = "Tell me more about your family."; 
    } 
    else if (statement.indexOf("brother") >= 0) 
    { 
     response = "I have a brother too!"; 
     } 
    else if (statement.indexOf("cat") >= 0 
      || (statement.indexOf("rabbit") >= 0)) 
    { 
    response = "Tell me more about your pets."; 
    } 
    else if (statement.indexOf("dog") >= 0) 
    { 
     response = "I wish I had a dog."; 
     } 
    else if (statement.indexOf("Mrs") >= 0 
      || statement.indexOf("Ms") >= 0 
      || statement.indexOf("Fisher") >= 0 
      || statement.indexOf("Zaengle") >= 0) 
      { 
     response = "They sound like a good teacher"; 
    } 
    else if (statement.indexOf("Mr") >= 0) 
    { 
     response = "Most of my teachers are mr's"; 
     } 
    else 
    { 
     response = getRandomResponse(); 
    } 
    return response; 
} 

}

private String getRandomResponse() 
{ 
    final int NUMBER_OF_RESPONSES = 6; 
    double r = Math.random(); 
    int whichResponse = (int)(r * NUMBER_OF_RESPONSES); 
    String response = ""; 

    if (whichResponse == 0) 
    { 
     response = "Interesting, tell me more."; 
    } 
    else if (whichResponse == 1) 
    { 
     response = "Hmmm."; 
    } 
    else if (whichResponse == 2) 
    { 
     response = "Do you really think so?"; 
    } 
    else if (whichResponse == 3) 
    { 
     response = "You don't say."; 
    } 
    else if (whichResponse == 4) 
    { 
     response = "Ah."; 
    } 
    else if (whichResponse == 5) 
    { 
     response = "Yeah."; 
    } 

    return response; 
} 

} }

+0

忘了說,錯誤在行中被觸發private String getRandomResponse() –

+0

你使用什麼文本編輯器/ IDE?一個體面的人會顯示匹配的括號/括號。 – StephaneM

+4

在StackOverflow上尋找不匹配的括號並不是可持續編程的方法。如果你使用IDE(你應該),你可以格式化代碼。這樣做會告訴你縮進突然出現的位置 - 這就是你錯過了某些東西的地方。 –

回答

0

你有超過1個錯誤:

  1. remvoe的;在結束方法getGreeting()

  2. 在方法getResponse()變量String response被聲明兩次!刪除其中一條聲明

  3. getResponse()方法的末尾,您確實有一個花括號太多!

  4. 在程序的最後,你確實有一個花括號!


沒有語法錯誤,但可能是一個語義錯誤:

if (statement.length() == 0); 
{ 
    response = "Say something, please."; 
} 

拿在if後分號(;)的密切關注!這分號顯示一個空語句,並{ response = ... }就一定會執行,不僅在if的條件爲真


這些錯誤很容易找到我,因爲我使用的是正確的IDE,它告訴我哪裏有塊我的語法錯誤是!

你應該考慮使用像Eclipse或其他任何IDE!

+0

哦,我的天哪,謝謝!你是一個救生員,當定時器完成後會接受。 –