新手写代码,可能比较繁复
public class book {
public static void main(String[] args) throws book_dontexist, error_command
{
book book1=new book();
book1.borrow_book1();
}
public void borrow_book1() throws book_dontexist, error_command
{
//book book1 = new book();
System.out.println("1-按照名称查找图书;2-按照序号查找图书");
Scanner input=new Scanner(System.in);
int command=input.nextInt();
if(command!=1&&command!=2)
{
try {
throw new error_command("命令输入错误!请根据提示输入命令");
}
catch(error_command m) {
}
finally {
borrow_book1();
}
}
String[] library ={"小王子","玻璃城堡","悲惨世界","心理罪","杀死一只知更鸟"};
if(command==1)
{
System.out.println("输入图书名称");
int m=0;
Scanner input1=new Scanner(System.in);
String bookname=input1.next();
for(int i=0;i<library.length;i++)
{
if(bookname.contentEquals(library[i]))
{
m++;
System.out.println("book:"+bookname);// System.out.println(library[i]);
}
if((i==library.length-1)&&(m==0))
{
try {
throw new book_dontexist("图书不存在");
}
catch(book_dontexist e) {
}
finally {
borrow_book1();
}
}
}
}
if(command==2)
{
System.out.println("输入图书序号");
int m=0;
Scanner input2=new Scanner(System.in);
int num=input2.nextInt();
if(num<library.length)
{
System.out.println("book:"+library[num]);// System.out.println(library[i]);
}
else
{
try {
throw new book_dontexist("图书不存在");
}
catch(book_dontexist e) {
}
finally {
borrow_book1();
}
}
}
}
}