終于做完了,大家?guī)兔纯从心睦镞€需要優(yōu)化完善的
//這是測試類部分,自定義異常類大家都一樣,就不貼了:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t=new Test();
t.borrow();
}
public void borrow(){
String[] books={"西游記","三國演義","水滸傳","紅樓夢"};
Scanner input=new Scanner(System.in);
System.out.println("*******歡迎使用圖書館借書系統(tǒng)*******");
System.out.println("1. 按照書名查找圖書? ? ? ?2.按照序號查找圖書");
try{
int num=input.nextInt();
if(num==1){
System.out.println("請輸入書名:");
String name=input.next();
for(int i=0;i<books.length;i++){
if(books[i].equals(name)){
System.out.println("找到圖書---"+books[i]);
}else{
throw new NobookException("圖書不存在,請重新輸入");
}
}
}else if(num==2){
System.out.println("請輸入序號:");
int no=input.nextInt();
if(no>0&&no<=books.length){
System.out.println("找到圖書---"+books[no-1]);
}else{
throw new NobookException("圖書不存在,請重新輸入");
}
}else{
throw new ErrorCodeException("命令錯誤,請重新輸入");
}
}catch(NobookException e){
System.out.println(e.getMessage());
borrow();
}catch(ErrorCodeException e){
System.out.println(e.getMessage());
borrow();
}catch(InputMismatchException e){
System.out.println("請按提示重新輸入!");
borrow();
}
}
}
2019-04-01
總體上說沒有大問題,就是有一個地方有個小問題,按照書名找書那里,拋出“圖書不存在”異常的時候(throw new NobookException("圖書不存在,請重新輸入");),是在for語句里面,程序無論如何都會拋出異常。
因為當(dāng)你輸入書名是“西游記”的時候,當(dāng)i循環(huán)到等于1?的時候拋出異常,當(dāng)你輸入的書名不是“西游記”的時候,i=0馬上就拋出異常了。
2019-04-23
應(yīng)該定義一個變量,保存book[i]的值然后通過判斷該變量的值? 輸出圖書 不然代碼總是在for循環(huán)后拋出異常
2019-04-14
多處錯誤
2019-04-01
貼一下完整代碼塞,參考一下哈哈
2019-03-29
粘貼上來格式怎么成這樣了? 好亂