百度翻译对于大家来说并不陌生,本案例要求编写一个程序模拟百度翻译。用户输入英文之后搜索程序中对应的中文,如果搜索到对应的中文就输出搜索结果,反之给出提示。本案例要求使用Map集合实现英文与中文的存储。
package main;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String, String> word = new HashMap<String, String>();
word.put("apple","苹果");
word.put("banana","香蕉");
word.put("orange","橘子");
word.put("grape","葡萄");
word.put("pear","梨子");
System.out.println("请输入英文:");
Scanner sc =new Scanner(System.in);
String name =sc.next();
if(!word.containsKey(name))
{
System.out.println("抱歉,您输入的单词本词典没有!");
}
else{
String chinese = word.get(name);
System.out.println(name+ "翻译成中文意思为:" + chinese);
}
}
}