从命令行创建一个基本的聊天机器人

huangapple 未分类评论42阅读模式
英文:

Create a basic chat bot from the command line

问题

public class Main {
    public static void main(String[] args) {
        if (args.length == 0)
            System.out.println("Nothing to say?");
        else {
            StringBuilder input = new StringBuilder();
            for (String data : args)
                input.append(data).append(" ");

            input = new StringBuilder(input.toString().toLowerCase());

            int indexOfAre = input.indexOf("are");
            int indexOfYou = input.indexOf("you");
            int indexOfFavourite = input.indexOf("favorite");

            if (indexOfAre != -1 && indexOfYou != -1 && indexOfAre < indexOfYou)
                System.out.println("Fine, but could use some more power.");
            else if (indexOfFavourite != -1) {
                int spaceIndex = input.indexOf(" ", indexOfFavourite);
                int qmIndex = input.indexOf("?", spaceIndex);
                String str = input.substring(spaceIndex + 1, qmIndex);
                System.out.println(str.trim() + "? I'm only interested in transistors.");
            } else
                System.out.println("I didn't understand that.");
        }
    }
}

The issue with the "transistors" answer seems to be caused by the fact that you are checking for the presence of the word "favorite" in the input string, but you are interested in the word "favourite" (with the British spelling). To fix this, you should change the line:

int indexOfFavourite = input.indexOf("favorite");

to:

int indexOfFavourite = input.indexOf("favourite");
英文:

I have tried everywhere else to get help with my homework question, I really just need some help with this one. I'm supposed to have a no-Scanner ChatBot with:

-If the user entered nothing at the command line, show Nothing to say?

-If the user entered some variation of how are you today? show Fine, but could use some more power. As long as the input has the word "are" before the word "you", print this message. Make your program smart enough to accept variations of these words (see output).

-If the user entered some variation of what is your favorite something? Display something? I'm only interested in transistors.

-In all other cases, show I didn't understand that.

Here's my code so far:

public class Main
{
public static void main(String[] args)

{
    if(args.length == 0)
        System.out.println(&quot;Nothing to say?&quot;);
    else
    {
        StringBuilder input = new StringBuilder();
        for(String data: args)

            input.append(data).append(&quot; &quot;);

        input = new StringBuilder(input.toString().toLowerCase());

        int indexOfAre = input.indexOf(&quot;are&quot;);
        int indexOfYou = input.indexOf(&quot;you&quot;);
        int indexOfFavourite = input.indexOf(&quot;favourite&quot;);

        if( indexOfAre != -1 &amp;&amp; indexOfYou != -1 &amp;&amp; indexOfAre &lt; indexOfYou)

            System.out.println(&quot;Fine, but could use some more power.&quot;);

        else if(indexOfFavourite != -1)
        {
            int spaceIndex = input.indexOf(&quot; &quot;, indexOfFavourite);

            int qmIndex = input.indexOf(&quot;?&quot;, spaceIndex);

            String str = input.substring(spaceIndex+1,qmIndex);

            System.out.println(str.trim()+&quot;? I&#39;m only interested in transistors.&quot;);
        }
        else
            System.out.println(&quot;I didn&#39;t understand that.&quot;);
    }
}
}

The transistors answer doesn't seem to be working and I can't figure out why, please help

Thank you 从命令行创建一个基本的聊天机器人

huangapple
  • 本文由 发表于 2020年7月23日 09:07:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/63045353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定