为简单的ChatBot程序获取代码错误

时间:2013-12-09 17:55:01

标签: java chat chatbot

我在第二课c.getResponse收到错误 The method getResponse(String) is undefined for the type BotTest 如果有人想看看作业是什么,那么pdf: http://www.cs.stonybrook.edu/~tashbook/fall2013/cse110/project-1.pdf

  import java.util.*;

   public class ChatBot {
public String getResponse(String input) {
    int i = 0;
    int found = input.indexOf("you", i);
    if (found == -1)

        return "I'm not important. Let's talk about you instead.";


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

    if (x == 4) {
        return "Tell me more about" + " " + longestWord(input);
    }
    if (x == 5) {
        return "Why do you think" + " " + longestWord(input) + " "
                + "is important?";
    } else if (x > 5) {
        return "Now we are getting somewhere. How does" + " "
                + longestWord(input) + " " + "affect you the most";
    }
    else
        return "I don't understand";
}   


private String longestWord(String input) {
    String word, longestWord = "";
    Scanner turtles = new Scanner(input);
    while (turtles.hasNext()) {
        word = turtles.next();
        if (word.length() > longestWord.length())
            longestWord = word;
    }
    return longestWord;

}

}

第二类测试代码        import java.util。*;

public class BotTest {
public static void main(String[] args) {
    Scanner newturtles = new Scanner(System.in);
    System.out.println("What would you like to talk about?");
    String input = newturtles.nextLine();
    BotTest c = new BotTest();
    while (!input.toUpperCase().equals("GOODBYE")) {
        System.out.println(c.getResponse(input));
        input = newturtles.nextLine();
    }
  }
}

2 个答案:

答案 0 :(得分:2)

getResponse是为ChatBot而不是BotTest

定义的
ChatBot c = new ChatBot();

答案 1 :(得分:0)

Class BotTest确实没有.getResponse(String)函数。 ChatBot确实如此。