编写一异常处理程序,模拟地铁、机场进行危险品与违禁物品检查。程序循环接受输入字符串,检查字符串。大致要怎样的思路
9条回答 默认 最新
- sinJack 2022-05-05 15:49关注
import java.util.*; public class CheckTest { public static void main(String[] args) throws MyException { Scanner in=new Scanner(System.in); List<String> blist=new ArrayList<>(); blist.add("b"); blist.add("o"); blist.add("m"); List<String> hlist = new ArrayList<>(); hlist.add("h"); hlist.add("e"); hlist.add("r"); hlist.add("o"); hlist.add("i"); hlist.add("n"); while (true){ System.out.print("输入待检测商品:"); char[] chars = in.next().toCharArray(); Map<Character,Integer> map=new HashMap<>(); map.put('b',0); map.put('o',0); map.put('m',0); for (char c:chars) { if (blist.contains(c+"")){ map.put(c,map.get(c)+1); } } //判断 危险品 if (map.get('b')>1 && map.get('o')>0 &&map.get('m')>0){ throw new MyException("发现危险品,有危险品炸弹"); } Map<Character,Integer> map2=new HashMap<>(); map2.put('h',0); map2.put('e',0); map2.put('r',0); map2.put('o',0); map2.put('i',0); map2.put('n',0); for (char c:chars) { if (hlist.contains(c+"")){ map2.put(c,map2.get(c)+1); } } //判断违禁品 if (map2.get('h')>0 && map2.get('e')>0 && map2.get('r')>0 && map2.get('o')>0 && map2.get('i')>0 &&map2.get('n')>0){ throw new MyException("发现违禁品。。。"); } } } } class MyException extends Exception{ public MyException(String message) { super(message); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录