Java程序实现英语单词互译成中文单词
时间: 2023-09-20 19:06:26 浏览: 135
可以使用Java中的HashMap实现英语单词和中文单词之间的互译。首先将所有需要互译的单词及其对应的翻译存储在HashMap中,然后根据用户输入的单词,从HashMap中查询其对应的翻译,并返回给用户。代码示例如下:
```
import java.util.HashMap;
import java.util.Scanner;
public class WordTranslator {
public static void main(String[] args) {
HashMap<String, String> dictionary = new HashMap<String, String>();
dictionary.put("hello", "你好");
dictionary.put("world", "世界");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入要翻译的单词:");
String word = scanner.nextLine();
if (word.isEmpty()) break;
if (dictionary.containsKey(word)) {
System.out.println(dictionary.get(word));
} else {
System.out.println("无法翻译该单词。");
}
}
}
}
```