-1
對於我的AP CompSci項目,我需要編寫一個聊天機器人。對Java.contains()方法的困惑
對於第一步,它需要檢測特定的關鍵字,並適當地迴應
然而,即使是statement
參數不包含「不」,responce
不知何故最終總是設置爲「爲什麼這麼消極?」 。我不知道這是爲什麼happeneing,但我想它的相關的Java 方法,我使用
我的代碼如下
static String[] family = {"mother", "father", "brother", "sister"};
static String[] pets = {"cat", "dog", "parrot", "lizard"};
static String[] family = {"mother", "father", "brother", "sister"};
static String[] pets = {"cat", "dog", "parrot", "lizard"};
public static String getResponse(String statement) {
String response = "Interesting. Tell me more.";
for (int i = 0; i < family.length; i++) {
if (statement.contains(family[i])) {
response = "Tell me more about your family.";
}
}
for (int i = 0; i < pets.length; i++) {
if (statement.contains(pets[i])) {
response = "Tell me more about your pets.";
}
}
if (statement.contains("no")); {
response = "Why so negative?";
}
return response;
}
public static void main(String[] args) {
System.out.println(getResponse("yes"));
//This will still act like the input is "no"
}
我有關可怕格式道歉上市,我不偉大的StackOverflow的
所有幫助表示讚賞 - 我不知道爲什麼它不輸出「有意思,告訴我更多」像它應該是
謝謝!
您的代碼未列在下方。你有沒有試過閱讀javadoc?以下是String#contains(...)的相關鏈接http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contains-java.lang.CharSequence- – stuXnet
請發佈代碼 – TheAnimatrix
我對發佈代碼表示抱歉 - 我意外提前提出了問題。代碼現在在那裏,謝謝! – Hstuart