我是Java新手,這是我的聊天機器人代碼的一部分。當我從showMenu()
運行createQuestions()
它似乎不工作。 createQuestion()
所做的就是讓用戶創建問題並與自己聊天。我的Java Chatbot代碼不工作,我不知道爲什麼
問題就在這裏
歡迎光臨!
選擇你的選項:
1)添加問題
2)聊天(您需要添加問題第一)
3)瞭解更多關於城鎮
4)退出
您:1
創建問題...輸入'en d'如果你想停止 問題? (因爲我需要讀什麼用戶輸入並存儲爲問題並不在這裏暫停)
您:
多少迴應你想要什麼? :
//Start of ShowMenu():
txtChat.append("\nWelcome!\nChoose your option:");
txtChat.append("\n1)Add Questions\n2)Chat(You need to add question first)\n3)Know more about Towns\n4)Exit\n");
txtChat.append(">>>\n");
txtEnter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showMenu();
}//end actionPerformed
});//end actionListener
}//end TestinChatBot
public void showMenu() {
String choice;
do {
switch (choice) {
case "1":
createQuestions();
break;
case "2":
startChat();
break;
case "3":
knowtowns();
break;
case "4":
txtChat.append("\nFinally! I can play MapleStory! Sayonara!");
System.exit(0);
break;
default:
break;
}
} while (!choice.equals("4"));
}
public void createQuestions() {
txtChat.append("\nCreating questions...Type 'end' if you wish to stop\n");
do {
txtChat.append("Question? \n");
q = txtEnter.getText();
txtChat.append("You: " + q + "\n");
if (!q.contains("end")) {
txtChat.append("How many responses do you want? : ");
noOfResponses = Integer.parseInt(txtEnter.getText());
txtEnter.setText("");
String r[] = new String[noOfResponses];
if (noOfResponses > 0) {
for (int i = 0; i < noOfResponses; i++) {
txtChat.append("Response " + (i + 1) + ": ");
r[i] = txtEnter.getText();
txtEnter.setText("");
}
Chat newChat = new Chat(q, r);
addQuestion(newChat);
txtChat.append("\n" + Arrays.toString(r));
} else {
txtChat.append("Please enter a number bigger than 0");
}
} else {
showMenu();
}
} while (q.equalsIgnoreCase("end") == false);
}
,並且錯誤是這些
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
'choice'在哪裏被分配? –