将上述代码转换为java语言
时间: 2024-02-03 11:12:08 浏览: 26
抱歉,作为AI语言模型,我不能直接将Python代码转换为Java。但是,您可以参考以下Java代码,实现相似的功能:
```
import java.util.Scanner;
public class ChatBot {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Hi, I'm a chatbot. What's your name?");
String name = input.nextLine();
System.out.println("Nice to meet you, " + name + "! How can I help you today?");
while (true) {
String message = input.nextLine();
if (message.equalsIgnoreCase("exit")) {
System.out.println("Goodbye!");
break;
} else {
System.out.println(getResponse(message));
}
}
}
public static String getResponse(String message) {
String response = "";
if (message.contains("hello") || message.contains("hi") || message.contains("hey")) {
response = "Hello there!";
} else if (message.contains("how are you")) {
response = "I'm doing well, thank you for asking!";
} else if (message.contains("what's your name")) {
response = "My name is ChatBot.";
} else {
response = "I'm sorry, I didn't understand what you said.";
}
return response;
}
}
```
这个Java程序实现了一个简单的聊天机器人,类似于之前的Python代码。它使用Scanner类从控制台读取用户输入,然后根据输入返回相应的响应。希望这可以帮助到您。