首页 > 其他 > 详细

字典的异常

时间:2017-04-23 17:51:18      阅读:148      评论:0      收藏:0      [点我收藏+]
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("此单词不存在,请重新输入");
}
    }

}

 

字典的异常

原文:http://www.cnblogs.com/zyy1130/p/6753054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!