麻烦大家帮我看看,LookupForName()这个函数里面的catch怎么没有作用呢?其他代码正常。
package com.borrow.books.system;
import java.util.*;
public class BorrowBooksSystem {
String[] books = new String[]{"语文","数学","英语","物理","化学","政治","历史"};
public static void main(String[] args) {
CheckForInput();
}
public static void CheckForInput(){
BorrowBooksSystem borrowBooks = new BorrowBooksSystem();
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
Scanner scan = new Scanner(System.in);
try{
int input = scan.nextInt();
if (input == 1){
borrowBooks.LookupForName();
}
if(input == 2){
borrowBooks.LookupForNumber();
}else{
System.out.println("错误命令!请重新输入!!");
CheckForInput();
}
}catch(InputMismatchException e){
System.out.println("错误命令!请重新输入!");
}finally{
CheckForInput();
}
}
//按名称查找
public void LookupForName(){
System.out.println("输入图书名称:");
Scanner scan = new Scanner(System.in);
try{
String input = scan.nextLine();
for (int i = 0; i < books.length; i++){
if (input.equals(books[i])){
System.out.println("book:" + input);
}
}
}catch(Exception e){
System.out.println("图书不存在!");
}finally{
CheckForInput();
}
}
//按序号查找
public void LookupForNumber(){
System.out.println("输入图书序号:");
Scanner scan = new Scanner(System.in);
try{
int input = scan.nextInt();
System.out.println("book:" + books[input-1]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("图书不存在!");
}catch(InputMismatchException e){
System.out.println("错误命令!请重新输入!");
}finally{
CheckForInput();
}
}
}