用户输入英文后,程序给出相对应的中文意思,(单词与中文都需要提前输入),若程序中没有相对应的,则提示不存在
此案例只有Test测试一个类
Test类
import java.util.*;
public class Test {
public static void main(String[] args) {
HashMap<String,String>word =new HashMap<String,String>();
while (true){
word.put("apple","苹果");
word.put("banana","香蕉");
word.put("book","书");
word.put("telephone","电话");
word.put("cat","猫");
word.put("dog","狗");
word.put("student","学生");
word.put("teacher","老师");
Scanner sc =new Scanner(System.in);
System.out.println("请输入要翻译的单词");
String name =sc.next();
//判断输入的单词是否在map集合中作为键符储
if (word.containsKey(name)){
String value =word.get(name);
System.out.println(name+"该单词翻译成中文后是 "+value);
}else {
System.out.println("单词不存在,无法翻译");
break;
}
}
}
}