发一下我的答案
public class ChaShu {
public static void main(String[] args) {
ChaShu cs = new ChaShu();
cs.FindBook();
}
public void FindBook() {
while(true) {
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
Scanner sc = new Scanner(System.in);
try {
int i =sc.nextInt();
if(i==1) {
String bookname = null;
First(bookname );
}
if(i==2) {
int a = 0;
Second(a);
}else{
System.out.println("输入错误,请重新输入");
}
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
public void First(String bookname) {
System.out.println("请输入图书名称:");
String book[] = {"高数","英语","语文"};
Scanner sc = new Scanner(System.in);
bookname = sc.nextLine();
try {
int a=1000000;
for(int i=0;i<book.length;i++) {
if(bookname.equals(book[i])) {
System.out.println("book:"+book[i]);
a=-1;
}
}
if(a==1000000) {
throw new Exception();
}
}catch(Exception e) {
System.out.println("图书不存在");
}
}
public void Second(int a) {
System.out.println("请输入图书id");
Scanner sc = new Scanner(System.in);
String book[] = {"高数","英语","语文"};
a = sc.nextInt();
if(a>0 && a<=book.length) {
System.out.println("book:"+book[a-1]);
}else {
System.out.println("图书不存在");
}
}
}