no worriesg 2022-05-05 14:08 采纳率: 75%
浏览 102
已结题

异常处理程序Java

编写一异常处理程序,模拟地铁、机场进行危险品与违禁物品检查。程序循环接受输入字符串,检查字符串。大致要怎样的思路

  • 写回答

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);
        }
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(8条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月5日
  • 已采纳回答 5月5日
  • 创建了问题 5月5日

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据