感觉不需要抛出
package com.qmx;
import java.util.Scanner;
public class FindBook {
String book[] = {"高数","数据结构"};
public static void main(String[] args) {
// TODO Auto-generated method stub
FindBook fb = new FindBook();
fb.findb();
}
public void findb() {
try {
System.out.println("输入命令: 1-按照名称查找图书; 2-按照序号查找图书");
Scanner input1 = new Scanner(System.in);
int type = input1.nextInt();
chooseBookBy(type);
} catch (Exception e) {
System.out.println("命令输入错误!请根据提示输入数字命令");
findb();
}
}
public void chooseBookBy(int type) {
if(type == 1) {
System.out.println("输入图书名称");
Scanner input = new Scanner(System.in);
String bookName = input.nextLine();
if(container(book, bookName) == true) {
System.out.println("book:" + bookName);
} else {
System.out.println("图书不存在");
findb();
}
} else {
try {
System.out.println("输入图书序号");
Scanner input = new Scanner(System.in);
int index = input.nextInt();
if(index>=0 && index < book.length) {
System.out.println("book:" + book[index]);
} else {
System.out.println("图书不存在");
findb();
}
} catch (Exception e) {
System.out.println("命令输入错误!请根据提示输入数字命令");
chooseBookBy(2);
}
}
}
public boolean container (String[] arr, String str) {
for(String s: arr) {
if(s.equals(str)) {
return true;
}
}
return false;
}
}