使用JAVA实现上述问题
时间: 2024-02-03 07:12:24 浏览: 29
以下是使用Java实现的代码:
```java
import java.util.Scanner;
public class Chatbot {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("你好,我是一个聊天机器人。请问有什么需要帮助的吗?");
String question = input.nextLine();
while (!question.equals("再见")) {
String answer = getAnswer(question);
System.out.println(answer);
question = input.nextLine();
}
System.out.println("再见,祝你有美好的一天!");
}
public static String getAnswer(String question) {
if (question.contains("你好")) {
return "你好,有什么需要帮助的吗?";
} else if (question.contains("天气")) {
return "今天的天气怎么样?";
} else if (question.contains("时间")) {
return "现在是几点钟?";
} else if (question.contains("谁")) {
return "我不清楚,请问你想了解哪个人?";
} else {
return "抱歉,我不理解你的问题。";
}
}
}
```
这个程序会不断地向用户询问问题,并根据问题返回不同的答案。如果用户输入“再见”,程序会结束并退出。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)