package?com.imooc.exception;
import?java.util.Arrays;
import?java.util.Scanner;
public?class?BorrowBook?{
????public?static?void?main(String[]?args)?{
????????BorrowBook?borrowBook=new?BorrowBook();
???????borrowBook.Borrow();
????}
????public?void?Borrow(){
????????//?書號分別對應(yīng)的是1,2,3,4
????????String[]?books={"西游記","紅樓夢","三國演義","水滸傳"};
????????System.out.println("歡迎使用圖書管理系統(tǒng)!");
????????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();
???????????????boolean?book=?Arrays.asList(books).contains(bookname);
???????????????if?(book==true){
???????????????????System.out.println(bookname+"借書成功!");
???????????????}else?{
???????????????????throw?new?NoBookException("此書不存在");
???????????????}
???????????}
???????????if?(num==2){
???????????????System.out.println("請輸入書號:");
???????????????Scanner?input2=new?Scanner(System.in);
???????????????int?shuhao=input2.nextInt();
???????????????????if?(shuhao>0?&&?shuhao<=books.length?){
???????????????????????System.out.println(books[shuhao-1]+"借書成功!");
???????????????????}else{
???????????????????????throw?new?NoBookException("查無此書!");
???????????????????}
???????????}
????????}catch?(NoBookException?e){
????????????System.out.println(e);
????????????System.out.println();
????????????Borrow();
????????}
????}
}
2019-07-28
你的shuhao應(yīng)該>=0,就比如36行,應(yīng)該這么寫:
if?(shuhao>=0??&& shuhao<books.length?)。因為數(shù)組下標(biāo)從0開始2019-07-23
這個序號是自動就給定好了的嗎?
2019-07-21
package?com.imooc.exception;?import?java.util.Arrays;import?java.util.Scanner;?public?class?BorrowBook?{????public?static?void?main(String[]?args)?{????????BorrowBook?borrowBook=new?BorrowBook();???????borrowBook.Borrow();????}?????public?void?Borrow(){????????//?書號分別對應(yīng)的是1,2,3,4????????String[]?books={"西游記","紅樓夢","三國演義","水滸傳"};????????System.out.println("歡迎使用圖書管理系統(tǒng)!");????????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();???????????????boolean?book=?Arrays.asList(books).contains(bookname);???????????????if?(book==true){???????????????????System.out.println(bookname+"借書成功!");???????????????}else?{???????????????????throw?new?NoBookException("此書不存在");???????????????}???????????}???????????if?(num==2){???????????????System.out.println("請輸入書號:");???????????????Scanner?input2=new?Scanner(System.in);???????????????int?shuhao=input2.nextInt();????????????????????if?(shuhao>0?&&?shuhao<=books.length?){???????????????????????System.out.println(books[shuhao-1]+"借書成功!");???????????????????}else{???????????????????????throw?new?NoBookException("查無此書!");???????????????????}???????????}????????}catch?(NoBookException?e){????????????System.out.println(e);????????????System.out.println();????????????Borrow();????????}????}}2019-05-08
你這個已經(jīng)挺簡單了,要是借書前加個循環(huán)輸出都有哪些書號+書名,就更好了