我正在爲我的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;
}
} }
忘了說,錯誤在行中被觸發private String getRandomResponse() –
你使用什麼文本編輯器/ IDE?一個體面的人會顯示匹配的括號/括號。 – StephaneM
在StackOverflow上尋找不匹配的括號並不是可持續編程的方法。如果你使用IDE(你應該),你可以格式化代碼。這樣做會告訴你縮進突然出現的位置 - 這就是你錯過了某些東西的地方。 –