如何将已实现的基础聊天机器人改造为基于String数组的版本?
Hey there, let's walk through this together—you're closer to solving this than you think! Let's break down your assignment and turn that vague confusion into working code.
作业需求 & 分步解决方案
第一部分:先搞定基础聊天机器人逻辑
Before jumping into arrays, let's get the core chatbot logic working first. The basic idea is:
- Use
JOptionPaneto grab user input - Check the input for keywords and return a matching response
- Loop the conversation until the user types an exit keyword (like "再见")
Here's a simple working version to start with:
import javax.swing.JOptionPane; public class BasicChatbot { public static void main(String[] args) { String userInput; do { // Get user input via JOptionPane userInput = JOptionPane.showInputDialog("嗨!有什么想聊的?输入“再见”结束对话~"); // Handle case where user clicks "Cancel" if (userInput == null) break; // Generate response based on input String response = getChatResponse(userInput); JOptionPane.showMessageDialog(null, response); } while (!userInput.equalsIgnoreCase("再见")); } private static String getChatResponse(String input) { // Check for keywords and return matching responses if (input.contains("你好") || input.contains("嗨")) { return "你好呀😊!"; } else if (input.contains("天气") || input.contains("温度")) { return "今天天气看起来不错哦!"; } else if (input.contains("再见") || input.contains("拜拜")) { return "拜拜啦,下次再聊!"; } else { return "抱歉,我不太明白你说的是什么😅"; } } }
第二部分:改造为String数组实现(核心升级)
Now, let's refactor this to use String arrays. The goal is to manage your keyword-response pairs with arrays, making the code more scalable (easier to add new keywords later). We'll use a 2D String array to store groups of keywords and their corresponding responses.
Here's the refactored version:
import javax.swing.JOptionPane; public class ArrayBasedChatbot { // 2D String array: [ [keyword1, keyword2], [response], [keyword3, keyword4], [response], ... ] private static final String[][] KEYWORD_RESPONSE_PAIRS = { {"你好", "嗨", "哈喽"}, {"你好呀😊!"}, {"天气", "温度", "下雨"}, {"今天天气看起来不错哦!"}, {"再见", "拜拜", "再见啦"}, {"拜拜啦,下次再聊!"} }; public static void main(String[] args) { String userInput; do { userInput = JOptionPane.showInputDialog("嗨!有什么想聊的?输入“再见”结束对话~"); if (userInput == null) break; String response = getResponseFromArray(userInput); JOptionPane.showMessageDialog(null, response); } while (!userInput.equalsIgnoreCase("再见")); } private static String getResponseFromArray(String input) { // Convert input to lowercase for case-insensitive matching String lowerInput = input.toLowerCase(); // Loop through the 2D array to find matching keywords for (int i = 0; i < KEYWORD_RESPONSE_PAIRS.length; i += 2) { String[] keywords = KEYWORD_RESPONSE_PAIRS[i]; String response = KEYWORD_RESPONSE_PAIRS[i + 1][0]; // Check each keyword in the current group for (String keyword : keywords) { if (lowerInput.contains(keyword.toLowerCase())) { return response; } } } // Default response if no keywords match return "抱歉,我不太明白你说的是什么😅"; } }
Key changes explained:
- String Array Management: We use
KEYWORD_RESPONSE_PAIRSto store all keyword groups and their responses. Adding a new topic is as simple as adding a new set of entries to the array—no need to mess with the logic code. - Case-Insensitive Matching: We convert both the user input and keywords to lowercase, so "你好" and "你好呀" will both trigger the greeting response.
- Scalability: This setup makes it easy to expand the chatbot later—just add more keyword-response pairs to the array.
Extra Tips:
- If you need to store conversation history, you can use a
String[]orArrayList<String>to keep track of past messages (e.g.,String[] chatHistory = new String[100];and increment an index each time the user sends a message). - Always handle
nullinput (when the user clicks "Cancel") to avoidNullPointerException.
内容的提问来源于stack exchange,提问作者Huseyin Yesiler
相关产品推荐
相关产品推荐

