各位大神:交流下作業(yè)心得,煩請大家提提意見!
以下為源文件:
package?three1;
import?java.util.InputMismatchException;
import?java.util.Scanner;
import?com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
public?class?LibrarySystem?{
//?圖書館現(xiàn)有書目
private?String[]?books?=?{"高數(shù)","大物","英語"};
//?按序號查找圖書
public?void?findABook(?int?num?){
try{
System.out.println("book:"?+?books[num]);
}catch?(InputMismatchException?e){
System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");
}
catch?(ArrayIndexOutOfBoundsException?e){
System.out.println("圖書不存在!");
}
}
//?按書名查找圖書
public?boolean?findABook(?String?bookName?){
for(?String?book:books){
if(book.equalsIgnoreCase(bookName)){
System.out.println("book:"?+?book);
return?true;
}
}
System.out.println("圖書不存在!");
return?false;
}
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
LibrarySystem?ls?=?new?LibrarySystem();
Scanner?sc?=?new?Scanner(System.in);
while(sc?!=?null){?
System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書。");
try{
int?commandNum?=?sc.nextInt();
sc.nextLine();
switch?(commandNum)?{
case?1:
System.out.println("輸入圖書名稱:");
String?bookName?=?sc.nextLine();
ls.findABook(bookName);
break;
case?2:
System.out.println("輸入圖書序號:");
int?bookNum?=?sc.nextInt();
sc.nextLine();
ls.findABook(bookNum);
break;
default:
//輸入整數(shù)范圍有誤,拋出異常
throw?new?Exception("0命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");
}
}catch(InputMismatchException?e){
System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");
sc.nextLine();
}catch?(Exception?e){
//e.printStackTrace();
System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");
}
}
}
}
2016-07-21
怎么沒用throws申明異常,就直接用throw拋出異常了?
2016-07-15
ls.findABook(bookNum);這句話的意思是?
2016-06-30
sc.nextLine();這是什么意思?
2016-06-22
嗯,運用的很棒!