import java.util.Scanner;
public class SeachBookMachine {
static void method1(String xuhao[],String shuming[]){
Scanner input=new Scanner(System.in);
System.out.println("请输入图书名称:");
try {
String booksm=input.next();
String bool="null";
for(int i=0;i<shuming.length;i++){
if(shuming[i].equals(booksm)){
System.out.println(xuhao[i]+":"+shuming[i]);
bool="not null";
}
}
if(bool.equals("null")){
throw new NoExistException("圖書不存在!");
}
} catch (NoExistException e) {
// System.out.println(e);
e.toshow();
method1(xuhao, shuming);
}
}
static void method2(String xuhao[],String shuming[]){
try {
Scanner input=new Scanner(System.in);
System.out.println("请输入图书序號:");
String bookxh=input.nextLine();
String bool="null";
for(int i=0;i<xuhao.length;i++){
if(xuhao[i].equals(bookxh)){
System.out.println(xuhao[i]+":"+shuming[i]);
bool="not null";
}
}
if(bool=="null")
throw new SlopOverException("圖書不存在~");
} catch (SlopOverException e) {
// System.out.println(e);
e.toshow();
method2(xuhao, shuming);
}
}
public static void main(String []args) {
String xuhao[]={"A1","B1","B2","B3","C1","D1"};
String shuming[]={"語文","數學","數學習題","數學試卷","英語","音樂"};
boolean choice=true;
Scanner input=new Scanner(System.in);
while(choice){
try {
System.out.println("輸入命令:1-按照書名查找圖書 2-按照序號查找圖書");
int choi=input.nextInt();
if(choi>2||choi<1)
throw new NumberFormatException("请输入数字1或2!");
switch (choi) {
case 1:
method1(xuhao, shuming);
break;
case 2:
method2(xuhao, shuming);
break;
}
choice=false;
System.out.println("你還想繼續查詢碼?(true or false)");
choice=input.nextBoolean();
}
catch (NumberFormatException e) {
System.out.println(e);
input.nextLine();
continue;
}catch (Exception e) {
System.out.println(e+":输入命令错误,请按照提示输入!");
input.nextLine();
continue;
}
}
}
}
class NoExistException extends Exception{
String string;
NoExistException(String stri){
string=stri;
}
void toshow(){
System.out.println(string+"请检查书名是否正确...");
}
}
class SlopOverException extends Exception{
String string;
SlopOverException(String stri){
string=stri;
}
void toshow(){
System.out.println(string+"请检查序号是否正确...");
}
}
点击查看更多内容
9人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦