迷你 java 聊天机器人

Mini java chatbot

我是 Java 的新手,正在尝试制作一个简单的迷你聊天机器人,通过用户输入和 if/else 语句回答 3 个问题。

我不知道我是否正确,但我已经走到这一步了:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Hello there! It's Chatbot here. How can I help you?");
        boolean running = true;
        while (running == true) {
            System.out.println(" ");
            String currentLine = input.nextLine();
            if (input.equals("Hi")) {
                System.out.println("Hi there! Any way I can help you?");
            } else if (input.equals("What do you eat?")) {
                System.out.println("I eat only your battery and internet!");
            } else if (input.equals("Who made you?")) {
                System.out.println("I was made thanks to Reinis!");
            } else if (input.equals("Bye")) {
                System.out.println("Bye! Have a nice day!");
            } else {
                System.out.println("Sorry! I am new to this and I didn't understand you. Try to ask me in a different way!");
            }
        }
    }
}

当我运行它一切都转到if语句时,你能指出我错误的正确方向吗?也许创建它的更好方法是什么。 java 是否有包含用户输入选项的单词?我不是要代码,只是为了指点,谢谢!

问题是您使用的变量 input 与 String 和 Scanner 相同,请像这样更改变量的名称:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Hello there! It's Chatbot here. How can I help you?");
        boolean running = true;
        while (running == true) {
            System.out.println(" ");
            String currentLine = input.nextLine();
            switch(currentLine) {
                case 'Hi':
                    System.out.println("Hi there! Any way I can help you?");
                    break;
                case 'What do you eat?':
                    System.out.println("I eat only your battery and internet!");
                    break;
                case 'Who made you?':
                    System.out.println("I was made thanks to Reinis!");
                    break;
                case 'Bye':
                    System.out.println("Bye! Have a nice day!");
                    break;
                default:
                    System.out.println("Sorry! I am new to this and didn't understand. Try to ask me in a different way!");
                    break;
            }
        }
    }
}