用户输入英文,控制台输出对应中文。 1.要求使用Map存储英文字典,只要求10个单词即可。 2.要求可以循环使用,输入x退出程序。 3.如果查不到,提示字典中不包含该单词。 java
时间: 2024-03-05 07:49:04 浏览: 16
好的,我明白了。这是一个简单的英汉词典程序,我可以帮助你编写。
你可以参考下面的代码实现:
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class EnglishChineseDictionary {
public static void main(String[] args) {
Map<String, String> dictionary = new HashMap<>();
dictionary.put("hello", "你好");
dictionary.put("world", "世界");
dictionary.put("java", "Java编程语言");
dictionary.put("computer", "计算机");
dictionary.put("book", "书");
dictionary.put("music", "音乐");
dictionary.put("game", "游戏");
dictionary.put("movie", "电影");
dictionary.put("apple", "苹果");
dictionary.put("orange", "橙子");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入要查询的单词(输入x退出程序):");
String word = scanner.nextLine().toLowerCase(); // 转换为小写字母
if (word.equals("x")) {
break;
}
String translation = dictionary.get(word);
if (translation == null) {
System.out.println("字典中不包含该单词!");
} else {
System.out.println(word + " 的中文翻译是:" + translation);
}
}
scanner.close();
}
}
```
这个程序首先创建了一个`Map`对象`dictionary`,用于存储英文字典,包含了10个单词和对应的中文翻译。然后进入循环,每次提示用户输入要查询的单词,如果输入的是`x`,则退出程序。程序会先将用户输入的单词转换为小写字母,然后通过`Map`的`get`方法查找对应的中文翻译。如果找不到,则提示字典中不包含该单词;否则输出单词和对应的中文翻译。最后关闭`Scanner`对象,程序结束。
希望这个程序对你有所帮助!