0
      import java.util.*;

public class TestChatBot 
{
    public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        String x = input.nextLine();


        TestChatBot e = new TestChatBot();
        {
            String prompt = "What would you like to talk about?";
            System.out.println(prompt);

            String userInput = input.nextLine();

            while(!userInput.equals("Goodbye"))
            {
                System.out.println(e.getResponse());
                userInput = input.nextLine();
            }
        }

    }

public class ChatBot 
{

    public String getResponse(String input)
    {
        Scanner userInput = new Scanner(input);
        input = userInput.nextLine();

        longestWord(input);

        String keyword = "you";
        int you = input.indexOf(keyword);

        if (you >= 0)
            return "I'm not important. Let's talk about you.";

        if (input.length() <= 3)
            return "Maybe we should move on. Is there anything else you would like to 
                        talk about?";

        if (input.length() == 4)
            return "Tell me more about " + input;

        if(input.length() == 5)
            return "Why do you think " + input + "is important?";

        else 
            return "Now we're getting somewhere. How does " + input + "affect you the 
                most?";
    }

    private String longestWord(String x)
    {
        Scanner input = new Scanner(x);     

        String longest = "";

        String temp = input.next();

        while (input.hasNext())
        {
            if (temp.length() > longest.length())
                longest = temp;
        }   

        return longest;

        }




         }
    }

在我的 ChatBotTest 类中,它说我的 getResponse() 方法未为类 TestChatBot 定义......我真的不明白它为什么这么说,它阻止了我的代码运行。我对 Java 很陌生,所以对于糟糕/草率的编码感到抱歉。非常感谢任何帮助,谢谢!

4

1 回答 1

4
TestChatBot e = new TestChatBot();

应该

ChatBot e = new ChatBot();

TestChatBox没有getResponse()

此外,您getResponse需要一个字符串参数。我想你想传递userInput给它

System.out.println(e.getResponse(userInput));
于 2013-12-06T23:56:31.033 回答