用java编写模拟翻译器 用户输入英文,控制台输出对应中文。 1.要求使用Map存储英文字典,只要求10个单词即可。 2.要求可以循环使用,输入x退出程序。 3.如果查不到,提示字典中不包含该单词。
时间: 2024-03-16 18:48:11 浏览: 25
好的,我可以为您解答这个问题。以下是Java代码实现:
```java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Translator {
public static void main(String[] args) {
Map<String, String> dictionary = new HashMap<>();
dictionary.put("hello", "你好");
dictionary.put("world", "世界");
dictionary.put("computer", "计算机");
dictionary.put("book", "书籍");
dictionary.put("music", "音乐");
dictionary.put("phone", "手机");
dictionary.put("internet", "互联网");
dictionary.put("game", "游戏");
dictionary.put("movie", "电影");
dictionary.put("language", "语言");
Scanner scanner = new Scanner(System.in);
String input = "";
while (!input.equals("x")) {
System.out.print("请输入要翻译的英文单词(输入x退出程序):");
input = scanner.nextLine().toLowerCase();
if (input.equals("x")) {
break;
}
if (dictionary.containsKey(input)) {
System.out.println("翻译结果为:" + dictionary.get(input));
} else {
System.out.println("字典中不包含该单词");
}
}
System.out.println("程序已退出");
}
}
```
这个程序使用了Map来存储英文字典,用户可以循环输入要翻译的英文单词,程序将会输出对应的中文翻译。如果用户输入的单词不在字典中,程序将会给出提示。用户可以输入"x"来退出程序。