类TestChatBot的方法是未定义的?

时间:2013-12-06 23:54:14

标签: java class methods chatbot

      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类中,它说我的getCesponse()方法未定义为类TestChatBot ...我真的不明白为什么它说这个并且它阻止了我的代码运行。我对Java很陌生,所以对于糟糕/草率编码我很抱歉。非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:4)

TestChatBot e = new TestChatBot();

应该是

ChatBot e = new ChatBot();

TestChatBox没有getResponse()

另外,你的getResponse需要一个String argmument。我想你想把userInput传递给它

System.out.println(e.getResponse(userInput));