交作业23333
异常类:
public class NotExistException extends Exception {
public NotExistException() {
}
public NotExistException(String message) {
super(message);
}
}
测试类:
import java.util.InputMismatchException;
import java.util.Scanner;
public class BookStore {
public static void main(String[] args) {
String[] book = {"语文","数学","英语","生物","物理","化学","地理"};
int flag = 0;
//查找方式
do {
System.out.println("输入命令:1-按照名称查找图书;2按照序号查找图书");
try {
Scanner sc = new Scanner(System.in);
flag = sc.nextInt();
String bookname ="";
boolean isExist = false;
//输入
if(flag==1) {//按名称查找图书
System.out.println("输入图书名称");
String name = sc.next();
bookname = name;
for(int i = 0;i < book.length;i++) {
if(book[i].equals(name)){
isExist = true;
break;
}
}
}else {//按序号查找图书
System.out.println("输入图书序号");
int i = sc.nextInt();
if(i>0 && i<book.length) {
bookname = book[i-1];
isExist = true;
}
else
isExist = false;
}
if(isExist)
System.out.println("book:"+bookname);
else {
NotExistException ex = new NotExistException();
throw ex;
}
}catch(InputMismatchException e) {
flag = 0;
System.out.println("命令输入错误!请根据提示输入数字命令!");
}catch(NotExistException e) {
flag = 0;
System.out.println("图书不存在!");
}
}while(flag != 1 && flag !=2);
}
}