设计一个程序,基于泛型Map实现10个英文单词的汉语翻译,即通过单词得到它的中文含义。
时间: 2023-06-01 17:01:28 浏览: 71
以下是一个Java程序,使用泛型Map实现10个英文单词的汉语翻译:
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class EnglishToChineseTranslator {
public static void main(String[] args) {
// 创建一个Map,存储英文单词和对应的中文翻译
Map<String, String> dictionary = new HashMap<>();
dictionary.put("apple", "苹果");
dictionary.put("banana", "香蕉");
dictionary.put("orange", "橙子");
dictionary.put("grape", "葡萄");
dictionary.put("watermelon", "西瓜");
dictionary.put("pear", "梨子");
dictionary.put("pineapple", "菠萝");
dictionary.put("strawberry", "草莓");
dictionary.put("melon", "甜瓜");
dictionary.put("kiwi", "猕猴桃");
// 从控制台读取用户输入的英文单词
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个英文单词:");
String word = scanner.nextLine().toLowerCase(); // 转换为小写字母
// 查找对应的中文翻译
String translation = dictionary.get(word);
if (translation != null) {
System.out.println(word + " 的中文含义是:" + translation);
} else {
System.out.println("对不起,没有找到 " + word + " 的中文翻译。");
}
}
}
```
运行程序后,用户可以输入一个英文单词,程序会输出对应的中文含义。如果输入的单词不在Map中,程序会提示没有找到对应的中文翻译。