百思不得其解,当图书名称输入错误时不能重新执行bookName()方法
package librarySystem; import java.util.*; public class LibrarySystem { //自定义NoBookException异常类 public static class NoBookException extends Exception{ public NoBookException(){} public NoBookException(String message){ super(message); } } public static String[] books={"高数","线代","几何","生物","编程"}; public static void main(String[] args) { System.out.println("序号.\t书名"); for (int i=1;i<=5;i++){ System.out.println(i+".\t\t"+books[i-1]); } LibrarySystem lib=new LibrarySystem(); lib.test(); } public void test(){ try { System.out.println("请选择:1-按照序号查找图书;2-按照名称查找图书"); int i = input(); switch (i) { case 1: bookNum(); break; case 2: bookName(); break; case -1: test(); break; default: throw new NoBookException("请输入1或2:"); } } catch(NoBookException e){ System.out.println(e.getMessage()); test(); } } public void bookNum(){ System.out.println("请输入图书序号:"); try { int i = input(); if (i > 0 & i <= books.length) System.out.println(books[i-1]); else throw new NoBookException("请输入正确的序号"); }catch(NoBookException e){ System.out.println(e.getMessage()); bookNum(); } } public void bookName(){ Scanner sc = new Scanner(System.in); try { System.out.println("请输入书名:"); String book= sc.next(); boolean flag=false; for (String i:books) { if (book.equals(i)) { flag=true; System.out.println(i); break; } } if(flag=false) { throw new NoBookException("请输入正确的书名:"); } }catch (NoBookException e){ System.out.println(e.getMessage()); bookName(); }catch (Exception e){ e.printStackTrace(); bookName(); } } public int input(){ Scanner sc = new Scanner(System.in); try { int i = sc.nextInt(); return i; }catch (Exception e){ System.out.println("请输入整数"); sc=new Scanner(System.in); return -1; } } }
第61行,当图书名称输入错误时不能重新执行bookName()方法,不知道哪出问题了