package zuizhong; public class Dict { DicItem[]dict=new DicItem[5]; class DicItem{ String en; String ch; public DicItem(String en, String ch) { super(); this.en = en; this.ch = ch; } } public Dict(){ dict[0]=new DicItem("book", "图书"); dict[1]=new DicItem("a", "一个"); dict[2]=new DicItem("she", "她"); dict[3]=new DicItem("he", "他"); dict[4]=new DicItem("hello", "你好"); } public String trans(String en) throws Exception{ for(DicItem e:dict){ if(en.equals(e.en)){ return e.ch; } } throw new Exception("这个单词不存在"); } }
package zuizhong; public class TestDic { public static void main(String[] args) { Dict d=new Dict(); try { String cn=d.trans("d"); System.out.println(cn); } catch (Exception e) { System.out.println("此单词不存在,请重新输入"); } } }
时间: 2024-11-07 23:18:29