MainBook
public class MainBook {
static String[] datas = { "JavaSE", "JavaEE", "Mysql", "JavaScript",
"Html5" };
static int status;
public static void main(String[] args) {
do {
try {
show();
} catch (BookException e) {
System.out.println(e.getMessage());
} catch (InputMismatchException e) {
System.out.println("输入类型不正确");
} catch (Exception e) {
System.out.println("未知异常");
}
} while (status == 0);
}
public static void show() throws BookException {
status = 0;
boolean flag = true;
do {
System.out.println("输入命令:1.按照名称查找书籍2.按照序号查找书籍(序号从0开始)0.退出程序");
Scanner sc = new Scanner(System.in);
int result = sc.nextInt();
if (result == 0) {
System.out.println("你已退出本程序,欢迎下次使用");
status = 1;
flag = false;
} else if (result == 1) {
System.out.print("请输入书籍名称: ");
String bookName = sc.next();
getByName(bookName);
} else if (result == 2) {
System.out.print("请输入书籍名称: ");
int bookNumber = sc.nextInt();
getByNumber(bookNumber);
} else {
throw new BookException("命令错误");
}
} while (flag);
}
public static void getByName(String bookName) throws BookException {
for (String bName : datas) {
if (bName.equals(bookName)) {
System.out.println("book: " + bName);
return;
}
}
throw new BookException("图书不存在");
}
public static void getByNumber(int bookNumber) throws BookException {
for (int i = 0; i < datas.length; i++) {
if (bookNumber == i) {
System.out.println("book: " + datas[i]);
return;
}
}
throw new BookException("图书不存在");
}
}
BookException
public class extends Exception {
public BookException() {
}
public BookException(String message) {
super(message);
}
}
效果图
点击查看更多内容
24人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦