求個大神指出錯誤...
import java.util.InputMismatchException;
import java.util.Scanner;
public class Book {
?public String books[]={"物理","化學","歷史","政治"};
?public static void main(String[] args) {
??Book Use=new Book();
??Use.test();
??
??
??// TODO Auto-generated method stub
?}
public? void test(){
Scanner input=new Scanner(System.in);
System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");
try{
?int a=input.nextInt();
?switch(a){
?case 1:
? System.out.println("請輸入圖書名稱");
? Scanner as=new Scanner(System.in);
? String name=as.next();
??? for(int i=0;i<books.length;i++){
???????? if(books[i].equals(name)){?
???? ?? System.out.println("book:"+books[i]);
???? ??? test();
????? }
??? }
???? ? System.out.println("圖書不存在,請重新輸入");
??? test();
?case 2:
??System.out.println("請輸入圖書序號");
??try{
??int d=input.nextInt();
??for(int i=0;i<books.length;i++){
???if(d==i+1){
???System.out.println("book"+books[i]);?
???}
??}
??test();
??}catch(InputMismatchException d ){
???System.out.println("輸入有誤,請輸入正確的圖書序號");
???test();
??}
?
?}
?
}catch(Exception e){
?System.out.println("命令輸入錯誤!請根據提示輸入數字命令");
?test();
}
?
}
?
}

2015-09-28
樓主的問題是啥?在我這運行的沒有報錯?。?/p>
2015-08-07
package?com.pun; import?java.util.InputMismatchException; import?java.util.Scanner; public?class?Book?{ public?static?void?main(String[]?args)?{ Book?Use=new?Book(); Use.test(); //?TODO?Auto-generated?method?stub } public??void?test(){ String[]?books={"物理","化學","歷史","政治"}; Scanner?input=new?Scanner(System.in); System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");? try{ int?a=input.nextInt(); switch(a){ case?1: System.out.println("請輸入圖書名稱"); Scanner?as=new?Scanner(System.in); String?name=as.next(); for(int?i=0;i<books.length;i++){ if(books[i].equals(name)){?? System.out.println("book:"+books[i]);? test(); } } System.out.println("圖書不存在,請重新輸入");? test(); case?2: System.out.println("請輸入圖書序號"); try{ int?d=input.nextInt(); for(int?i=0;i<books.length;i++){ if(d==i+1){ System.out.println("book"+books[i]);? } } test(); }catch(InputMismatchException?d?){ System.out.println("輸入有誤,請輸入正確的圖書序號"); test(); } } }catch(Exception?e){ System.out.println("命令輸入錯誤!請根據提示輸入數字命令"); test(); } } }