我正在为我的APCS课程写一个聊天机器人。我收到一个错误,错误:class,interface或enum expected,我发现通常在括号或括号不匹配时触发。我有轻微的阅读障碍,这使得试图找到这些地狱。如果有人会帮助我,我将非常感激。这是代码。 公共课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 :(得分:0)
你确实有一个以上的错误:
在方法;
getGreeting()
方法getResponse()
中的变量String response
被声明两次!删除其中一个声明
在getResponse()
方法的最后,你确实有一个大括号!
在程序的最后,你确实有一个大括号!
不是语法错误,但可能是语义错误:
if (statement.length() == 0);
{
response = "Say something, please.";
}
仔细查看;
之后的分号(if
)!这个分号显示一个空语句,并且只有当{ response = ... }
的条件为真时才会执行块if
这些错误很容易找到,因为我使用了一个适当的IDE,告诉我我的语法错误在哪里!
您应该考虑使用Eclipse或其他任何IDE!