作業的疑問
package?sentBook;
import?java.util.Arrays;
import?java.util.InputMismatchException;
import?java.util.Scanner;
public?class?sentBook?{
String[]?book?=?new?String[]{"你好,中國",?"九型人格",?"Java123",?"面向對象編程",?"美文如歌"};
Scanner?scan?=?new?Scanner(System.in);
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
sentBook?sent?=?new?sentBook();
sent.welcome();
}
/*歡迎界面?
?*同時對輸入進行校驗?
?*/
public?void?welcome(){
System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書;");
int?select?=?0;
try{
select?=?new?Scanner(System.in).nextInt();
}catch(InputMismatchException?e){
System.out.println("命令輸入錯誤!請按照提示輸入數字命令!");
this.welcome();
}
try{
if(select?==?1?){
this.sentName();
}else?if(select?==?2){
this.sentNum();
}else?if(select?==?0){
System.out.println("fuck!");
}
else{
throw?new?Exception();
}
}catch(Exception?e){
System.out.println("請按照提示輸入正確命令!");
this.welcome();
}
return;
}
/*
?*?按名稱查書
?*/
public?void?sentName(){
System.out.println("輸入圖書名稱:");
String?name?=?scan.next();
String?books?=?Arrays.toString(book);
if(books.indexOf(name)?!=?-1){
System.out.println("book:"?+?name);
}else{
System.out.println("圖書不存在!");
this.welcome();
}
}
/*
?*?按序列號查書
?*/
public?void?sentNum(){
System.out.println("輸入圖書序號:");
int?num?=?scan.nextInt();
if(num?<=?book.length+1){
System.out.println("book:"?+?book[num?-?1]);
}else{
System.out.println("圖書不存在!");
this.welcome();
}
}
}代碼如圖所示,現在有一個問題,具體的輸出如下:
輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書;
a
命令輸入錯誤!請按照提示輸入數字命令!
輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書;
1
輸入圖書名稱:
九型人格
book:九型人格
fuck!
最開始是因為發現一個問題,如果第一次輸入錯誤,那么在正確的輸入以及查詢結束后,會繼續循環一次查詢。利用斷點調試發現,第一次輸入錯誤并之后正確執行了一次查詢后,會繼續對輸入的select值進行判斷,而且是默認的0。如果把這個默認的select賦值去掉的話,程序會報錯。
想請教一下,怎樣才能在正確執行一次查詢后直接結束程序,而不是繼續對select值進行判斷。
2015-01-19
你的代碼錯了,第一步你輸入a后拋出異常,但被你捕獲了,所以先執行了welcome(),再接著往下執行,也就是說你輸入一個a時,你的if...else if同樣還是被執行了
2015-01-19
已修改代碼
package?sentBook; import?java.util.Arrays; import?java.util.InputMismatchException; import?java.util.Scanner; public?class?sentBook?{ String[]?book?=?new?String[]{"你好,中國",?"九型人格",?"Java123",?"面向對象編程",?"美文如歌"}; Scanner?scan?=?new?Scanner(System.in); public?static?void?main(String[]?args)?{ //?TODO?Auto-generated?method?stub sentBook?sent?=?new?sentBook(); sent.welcome(); } /*歡迎界面? ?*同時對輸入進行校驗? ?*/ public?void?welcome(){ System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書;"); int?select?=?0; try{ select?=?new?Scanner(System.in).nextInt(); if(select?==?1?){ this.sentName(); }else?if(select?==?2){ this.sentNum(); }else?if(select?==?0){ System.out.println("fuck!"); } else{ throw?new?Exception(); } }catch(InputMismatchException?e){ System.out.println("命令輸入錯誤!請按照提示輸入數字命令!"); this.welcome(); }catch(Exception?e){ System.out.println("請按照提示輸入正確命令!"); this.welcome(); } return; } /* ?*?按名稱查書 ?*/ public?void?sentName(){ System.out.println("輸入圖書名稱:"); String?name?=?scan.next(); String?books?=?Arrays.toString(book); if(books.indexOf(name)?!=?-1){ System.out.println("book:"?+?name); }else{ System.out.println("圖書不存在!"); this.welcome(); } } /* ?*?按序列號查書 ?*/ public?void?sentNum(){ System.out.println("輸入圖書序號:"); int?num?=?scan.nextInt(); if(num?<=?book.length){ System.out.println("book:"?+?book[num?-?1]); }else{ System.out.println("圖書不存在!"); this.welcome(); } } }2015-01-19
if...else if...本身就是正確執行一次后就不再執行別的判斷了,所以你這邊的問題應該是邏輯錯誤