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