花了一下午寫的借書系統(tǒng)的作業(yè),運行良好。但有兩個小問題,希望高手指點一下。
package?com.imooc.homework; import?java.util.Scanner; public?class?Initial?{ @SuppressWarnings("resource") public?static?void?main(String[]?args)?throws?Order,NoExistException{ //?TODO?自動生成的方法存根 //賦值:"C語言",?"數(shù)據(jù)結構",?"匯編語言",?"高數(shù)",?"大學語文",?"毛概" Book[]?books={new?Book(1,"C語言"),new?Book(2,"數(shù)據(jù)結構"),new?Book(3,"匯編語言"), new?Book(4,"高數(shù)"),new?Book(5,"大學語文"),new?Book(6,"毛概") }; while(true){ //開始查詢 //選擇查詢方式 System.out.println("***輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書"); ?????????Scanner?input=new?Scanner(System.in); ????????int?seekType; ????????try{ ???????? ?seekType=input.nextInt(); ????????}catch(Exception?e){ System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!"); input?=?new?Scanner(System.in); continue; }//排除輸入不為數(shù)字的情況 ???????? ????????if(seekType==1){ ???????? //用圖書名尋找圖書 ??????? ???????? System.out.println("輸入圖書名稱:"); ???????? String?j=input.next();? ???????? for(int?i=0;i<books.length;i++){ ???????? if(j.equals(books[i].getBookName())){ ???????? System.out.println("book:"+books[i].getBookName()); ???????? return; ???????? } ???????? } ???????? System.out.println("圖書不存在"); ???????? continue; ?????? ????????} ????????else?if(seekType==2){ ???????? //用圖書序號尋找圖書 ???????? System.out.println("輸入圖書序號:"); ???????? int?k; ???????? ???????? try{ ???????? ??k=input.nextInt(); ???????? ?}catch(Exception?e){ ???????? System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!"); ????? input?=?new?Scanner(System.in); ????? continue; ???????? ?} ???????? ? ????????? ?for(int?i=0;i<books.length;i++){ ???????? if(books[i].getBookNum()==k){ ???????? System.out.println("book:"+books[i].getBookName()); ???????? return; ???????? } ???????? } ????????? System.out.println("圖書不存在"); ???????? continue; ???? ????????}else{ ???????? System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字1或2"); continue; ????????} ?? } } }
package?com.imooc.homework; //定義所有書的類 public?class?Book?{ private?int?bookNum;??????//書序號 private?String?bookName;??//書名 public?Book(){ } public?Book(int?bookNum,String?bookName){ this.bookNum?=?bookNum; this.bookName?=?bookName; } protected?int?getBookNum()?{ return?bookNum; } protected?void?setBookNum(int?bookNum)?{ this.bookNum?=?bookNum; } protected?String?getBookName()?{ return?bookName; } protected?void?setBookName(String?bookName)?{ this.bookName?=?bookName; } }
問題一:
——————————————————————————————————————
運行效果:
***輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書
1
輸入圖書名稱:
高數(shù)
圖書不存在
***輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書
——————————————————————————————————————
如上,當運行程序,依次輸入"1""高數(shù)”時,當輸入高數(shù)時光標默認是在“輸入圖書名稱”這一行的前面,那么當輸入完“高數(shù)”時,高數(shù)自動顯示到“輸入圖書名稱”這一行的下一行的,但調試時發(fā)現(xiàn),這時變量 j 里面存的就是“gaoshugao高數(shù)”這個數(shù)據(jù)串。但如果在輸入的時候,點擊下鼠標,把光標放在“輸入圖書名稱”這一行的下一行,那么變量 j 就賦值為“高數(shù)”,結果運行良好。
我的問題是:輸入時和光標的位置有關嗎?還是我的編程哪一點有問題?如何改進?
問題二:
——————————————————————————————————————————————
如上面的一段代碼:? ? ? ?
?Scanner input=new Scanner(System.in);
? ? ? ?int seekType;
? ? ? ?try{
? ? ? ? seekType=input.nextInt();
? ? ? ?}catch(Exception e){
? ????? System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");
????????input = new Scanner(System.in);
????????continue;
}//排除輸入不為數(shù)字的情況
——————————————————————————————————————————
當應該輸入數(shù)字,但如果輸入非數(shù)字,那么會出現(xiàn)異常。我捕捉到這個異常,輸出提示,并用了這個語句“input = new Scanner(System.in);”。請問這個語句有必要用嗎?我用是因為老師給的答案里是這么做的,但我不知道為什么要這么做?如果不這樣會出問題嗎?我的一個猜想是:老師的答案里,input是靜態(tài)static函數(shù)(老師用的是console這個變量),所以需要重新初始化一下,而我的是protect的,是不是就不需要?
因為水平低,這個作業(yè)做了一下午加半個晚上。希望提高自己的水平,但見識有限,希望高人能指點一下。
2015-07-26
我也是初學者,學到這里了。
問題一:
我也是這種情況,應該是軟件自身的原因吧;
問題二:
應該是這樣
另外我認為用if else?控制流程不太好,我剛開始也是這樣,后來發(fā)現(xiàn)如果輸入的數(shù)字是3或4或5或6.。。。,你會發(fā)現(xiàn)這個“異?!笔遣蹲讲坏降?,可以用switch選擇結構,default可以排除這個情況。我覺得異常一塊捕捉了更方便啊
這是我的代碼
import java.util.Scanner;
public class Book {
?static String [] books={"大學","數(shù)據(jù)結構","論語"};
?
?public static void main(String[] args) {
??// TODO Auto-generated method stub
??while(true){
??System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");
??Scanner s=new Scanner(System.in);
??try{
?? int a = s.nextInt();
?? switch(a){
?? case 1:
??? System.out.println("請輸入圖書名稱:");
??? String name = s.next();
???for(String o :books){
????if(o.equals(name)){
?????System.out.println("book:"+o);
???????? return ;
????}
???}
????System.out.println("該圖書不存在");
?????? break;
?? case 2:
???? System.out.println("請輸入圖書序號:");
???? int?number=s.nextInt();
???? for(String o:books)
????? if(o.equals(books[number-1])){
??????? System.out.println("book:"+o);
??????? return;
????? }
???? System.out.println("該圖書不存在:");
???? break;
?? default:
??? System.out.println("命令輸入錯誤!");
?? }
??}catch(Exception e)
??{
???System.out.println("命令出入錯誤,請根據(jù)提示輸入命令");
??}
?}
?}
}