作业写完了
package Demo;
import java.util.Scanner;
public class BorrowBook {
public static void main(String[] args) {
BorrowBook test=new BorrowBook();
test.bobo();
}
public void bobo(){
String Books[]= {"数据结构","高数","java语言","英语"};
System.out.println("*******欢迎来到慕课网借书系统*******");
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
Scanner input=new Scanner(System.in);
try{
int num=input.nextInt();
if(num==1){
System.out.println("输入图书名称:");
Scanner input1=new Scanner(System.in);
String Bookname =input1.nextLine();
int a=-1;
for(int i=0;i<Books.length;i++) {
if(Books[i].equals(Bookname)) {
System.out.println("book:"+Books[i]);
System.out.println("*************您借书成功*************");
a=1;
}
}
if(a==-1) {
throw new NobookException("图书不存在!");
}
}else if(num==2){
System.out.println("输入图书名序号:");
Scanner input2=new Scanner(System.in);
int Booknum =input2.nextInt();
if(Booknum>0&&Booknum<=Books.length) {
System.out.println("book:"+Books[Booknum-1]);
System.out.println("*************您借书成功*************");
}else {
throw new NobookException("图书不存在!");
}
}
}catch(NobookException e) {
System.out.println(e.getMessage());
bobo();
// }catch(ErrorCodeException e){
// System.out.println("命令输入错误!请根据提示输入数字命令");
// bobo();
}catch(Exception e){
System.out.println("命令输入错误!请根据提示输入数字命令");
bobo();
}
}
}