You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Eclipse的Java简易ChatterBot开发:问号触发逻辑问题求助

Fixing the Question Mark Trigger Issue in Your ChatterBot

Hey there! Let's get that question mark handling working consistently for your Java ChatterBot. The problem you're seeing—where the response only triggers on the first input—almost certainly means your check for the ? character is only running once, instead of every time the user sends a message.

Here's the Fix:

The core solution is to ensure your question mark check lives inside the loop that handles user input repeatedly. Let's break this down with code examples.

Step 1: Structure Your Code for Continuous Input

Wrap your input handling in an infinite loop (with an exit condition, like typing "quit") so every user message gets processed:

import java.util.Scanner;

public class ChatterBot {
    public static void main(String[] args) {
        Scanner userScanner = new Scanner(System.in);
        
        // Keep the bot running until user quits
        while (true) {
            System.out.print("You: ");
            String userInput = userScanner.nextLine().trim();

            // 1. Check for the question mark EVERY time a message is sent
            if (userInput.contains("?")) {
                System.out.println("ChatterBot: i will be asking the questions");
            }

            // 2. Handle your existing keyword-triggered questions
            if (userInput.contains("weather")) {
                System.out.println("ChatterBot: Do you prefer warm or cold weather?");
                // Don't forget to check for ? in follow-up inputs too!
                String followUp = userScanner.nextLine().trim();
                if (followUp.contains("?")) {
                    System.out.println("ChatterBot: i will be asking the questions");
                }
                // Add more follow-up logic as needed
            } else if (userInput.contains("hobby")) {
                System.out.println("ChatterBot: What's your go-to hobby when you're bored?");
            }

            // Exit condition to stop the bot
            if (userInput.equalsIgnoreCase("quit")) {
                System.out.println("ChatterBot: See you later!");
                break;
            }
        }
        userScanner.close();
    }
}

Key Things to Note:

  • Loop Placement: The question mark check is inside the while(true) loop, so it runs for every single input the user sends.
  • Follow-Up Checks: If your bot has multi-turn question sequences (like after the user mentions "weather"), add the same ? check to those follow-up input reads too—otherwise the trigger won't work mid-conversation.
  • Flexible Order: You can place the question mark check before or after your keyword logic, depending on if you want it to run alongside other responses or take priority.

Why Your Original Code Failed:

Chances are, your initial question mark check was outside the main input loop, or only ran once at the start of the program. By moving it into the loop that processes every user message, you ensure it triggers every time a ? is present.

内容的提问来源于stack exchange,提问作者Huseyin Yesiler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:32:16