為什么輸入存在的圖書名也是執(zhí)行異常?求大蛇指點(diǎn)
public class borrowBooks {
public static void main(String[] args) {
// 調(diào)用select方法
borrowBooks Select = new borrowBooks();
Select.select();
}
/**
*?
*/
public void select() {
String[][] bookList=new String[][] {{"1","水滸傳"},{"2","紅樓夢(mèng)"},{"3","三國(guó)演義"}};
//String[] bookList = new String[] {"java基礎(chǔ)","水滸傳","紅樓夢(mèng)","西游記","數(shù)據(jù)結(jié)構(gòu)"};?
System.out.println("輸入命令:1-按照?qǐng)D書名稱查詢 ? 2-按照?qǐng)D書編號(hào)查詢");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
try {
if(num==1) {
System.out.println("請(qǐng)輸入您要查找的圖書名稱:");
Scanner input1 = new Scanner(System.in);
String bookName=input1.nextLine();
//一維數(shù)組
/* boolean t=Arrays.asList(bookList).contains(bookName);
if(t) {
System.out.println("您輸入的圖書"+bookName+"存在!");
}else {
throw new NobookException();
}*/
//二維數(shù)組
for(String[] bookname:bookList) {
for(String book:bookname) {
if(book.equals(bookName)) {
System.out.println("您輸入的圖書"+book+"存在!");
}else ?{
throw new NobookException();
}
}
}
}else if(num==2) {
System.out.println("請(qǐng)輸入您要查找的圖書編號(hào)");
Scanner input2 = new Scanner(System.in);
int bookNum=input2.nextInt();
if(bookNum>=1&&bookNum<=bookList.length) {
System.out.println("您查找的圖書名稱為"+bookList[bookNum-1]);
}else {
throw new NobookException();
}
}else {
throw new CommandException();
}
}catch (NobookException e) {
// TODO: handle exception
System.out.println("您輸入的圖書不存在!請(qǐng)重新輸入");
select();
}catch (CommandException e) {
// TODO: handle exception
System.out.println("您輸入的命令有誤,請(qǐng)重新輸入");
select();
}
}
}
2019-03-18
以下語(yǔ)句執(zhí)行多次,書名不一樣時(shí),就拋出異常。
if(book.equals(bookName)) {
System.out.println("您輸入的圖書"+book+"存在!");
}else ?{
throw new NobookException();
}