字典的异常

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

}

 

转载于:https://www.cnblogs.com/zyy1130/p/6753054.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值