编写一异常处理程序,模拟地铁、机场进行危险品与违禁物品检查。程序循环接受输入字符串,检查字符串。大致要怎样的思路
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);
}
}
在while循环中,通过Scanner读入字符串,字符串跟违禁列表中的内容进行对比,如果在违禁列表中,就显示。
违禁列表可以用List或者字符串数组都可以
代码如下:
import java.util.Scanner;
public class Test {
public static void main(String[] args){
String[] wj = {"酒精","煤油","打火机","烟花爆竹","管制刀具"};//根据需要进行补充或者细分
Scanner sc = new Scanner(System.in);
boolean flag = true;
while(flag == true){
System.out.print("请输入物品名称:");
String wp = sc.next();
int i =0;
for(;i<wj.length;i++){
if(wp.equals(wj[i])){
System.out.println("违禁物品:"+ wp);
break;
}
}
if(i == wj.length)
System.out.println("非违禁物品。");
System.out.print("是否继续输入?(Y/N)");
String gg = sc.next();
if(gg.charAt(0) == 'N' || gg.charAt(0)== 'n')
flag = false;
}
}
}
随便用个能存字符串的容器保存下你的”违禁品“字符串,例如list、set、array等等。
然后循环读取输入,读到一行就在容器内查找有无该字符串,有则是违禁品。
1.明确有哪些属于违禁品,用一个集合保存下来,可以做成静态,提前初始化;
2.明确怎么算是违禁品,是名字一样,还是名字里含有某些关键字,单独抽成一个方法,传入输入的字符串,从集合中循环去判断,返回一个boolean;
3.定义Scanner , 然后while,确定退出条件,接收输入的字符串,调用第二步的判断方法;...
编写一异常处理程序,模拟地铁、机场进行危险品与违禁物品检查。程序循环接受输入字符串,检查字符串。若其中含有'b''o''m''b'四个字母(字母顺序无关)就抛出发现危险品异常,提示有危险品炸弹;若其中含有'h'’e’’r’’o’’i’’n'六个字母则抛出发现违禁品异常。如果没有异常,程序循环接受输入字符串。
public static void main(String[] args){
List<String> list = Arrays.asList("bomb","heroin");
Scanner sc = new Scanner(System.in);
while(true) {
System.out.print("请输入:");
String input = sc.next();
if (list.contains(input)) {
throw new RuntimeException("有危险品炸弹或违禁品");
}
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args){
String[] wj = {"酒精","煤油","打火机","烟花爆竹","管制刀具"};//根据需要进行补充或者细分
boolean flag = true;
while(flag){
System.out.print("请输入物品名称:");
Scanner sc = new Scanner(System.in);
String wp = sc.next();
for(int i =0;i<wj.length;i++){
if(wp.equals(wj[i])){
System.out.println("违禁物品:"+ wp);
break;
}
}
if(i == wj.length)
System.out.println("非违禁物品。");
System.out.print("是否继续输入?(Y/N)");
Scanner scc = new Scanner(System.in);
String gg = scc.next();
if("N".equalsIgnoreCase(gg))
flag = false;
}
}
}
实现代码如下
public class Answer7709463 {
public static void main(String[] args) {
// 首先要有一个危险品和违禁物品的列表
List<String> list = new ArrayList<>();
list.add("炸弹");
list.add("烟花");
Scanner scanner = new Scanner(System.in);
while(true) {
String input = scanner.nextLine();
if(list.contains(input)) {
System.out.println("这是危险品和违禁物品");
} else {
System.out.println("这不是危险品和违禁物品");
}
}
}
}
运行结果如下
请输入物品: 炸弹
这是危险品和违禁物品
请输入物品: 饼干
这不是危险品和违禁物品
如有帮助,请采纳,十分感谢!