如何让聊天机器人识别出空白回复?如何使其与多个空间一起使用?

时间:2017-10-22 15:47:53

标签: java

以下是我班上的说明:

目前,如果用户没有输入任何内容,程序会给出随机响应。使它成为一个空输入使程序响应,"请说些什么。"为了帮助您,请了解String类中的trim()方法。 (在教科书或在线查找。)请注意,如果你修剪()一个所有空格的字符串,你将得到一个空字符串作为结果。如果用户键入三个空格,请使用此选项以确保您做出适当的响应!

代码:

public class Magpie2
{
    /**
     * Get a default greeting   
     * @return a greeting
     */
    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 (findKeyword(statement,"no") >= 0)
        {
            response = "Why so negative?";
        }
        else if (findKeyword(statement,"mother") >= 0
                || findKeyword(statement,"father") >= 0
                || findKeyword(statement,"sister") >= 0
                || findKeyword(statement,"brother") >= 0)
        {
            response = "Tell me more about your family.";
        }
        else if (findKeyword(statement,"cat") >= 0
                || findKeyword(statement,"dog") >= 0)
        {
            response = "Tell me more about your pets.";
        }
        else if (findKeyword(statement,"Levine") >= 0)
        {
            response = "Tell me more about your instructor.";
        }
        else if (findKeyword(statement,"phone") >= 0
                || findKeyword(statement,"cellphone") >= 0
                || findKeyword(statement,"iphone") >= 0)
        {
            response = "I'm smarter than your phone.";
        }
        else if (findKeyword(statement,"I want to") >= 0)
        {

        }
        else
        {
            response = getRandomResponse();
        }
        return response;
    }

1 个答案:

答案 0 :(得分:0)

答案在解释中。

"请注意,如果你修剪()一个所有空格的字符串,结果会得到一个空字符串。"

扩展if / else语句以包含else,如果它检查条件使得statement.trim()等于""。