码迷,mamicode.com
首页 > 其他好文 > 详细

字典的异常

时间:2017-04-23 18:10:49      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:system   ack   turn   book   equals   for   void   his   item   

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("此单词不存在,请重新输入");
}
    }

}

 

字典的异常

标签:system   ack   turn   book   equals   for   void   his   item   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!