用Java写个例子
时间: 2024-02-03 17:13:38 浏览: 17
好的,以下是一个使用Java编写的示例程序:
```
import java.util.Scanner;
public class Chatbot {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String message = "";
System.out.println("Hello! I am your chatbot. What's your name?");
String name = scanner.nextLine();
System.out.println("Nice to meet you, " + name + "!");
while (!message.equals("bye")) {
System.out.print(name + ": ");
message = scanner.nextLine();
if (message.contains("hello") || message.contains("hi")) {
System.out.println("Chatbot: Hi there!");
} else if (message.contains("how are you")) {
System.out.println("Chatbot: I'm doing well, thank you for asking.");
} else if (message.contains("what's your favorite color")) {
System.out.println("Chatbot: My favorite color is blue.");
} else if (message.contains("bye")) {
System.out.println("Chatbot: Goodbye, " + name + "!");
} else {
System.out.println("Chatbot: I'm sorry, I didn't understand what you said.");
}
}
scanner.close();
}
}
```
这个程序实现了一个简单的聊天机器人,它会根据用户的输入给出不同的回复。用户可以输入“hello”或“hi”来打招呼,输入“how are you”来询问聊天机器人的状况,输入“what's your favorite color”来询问聊天机器人喜欢的颜色,输入“bye”来结束对话。如果用户输入了聊天机器人不理解的内容,聊天机器人会回复“I'm sorry, I didn't understand what you said.”