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);
}
}
效果图
點(diǎn)擊查看更多內(nèi)容
24人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦