指令输入正常,程序没问题。但是指令测试bug就跳过了怎么办,求大佬指点
package com.test.trycatch; import java.util.*; public class TryTest { static Scanner sin = new Scanner(System.in); static BookFindTry[] books = {new storyBook(1,"西游记"),new storyBook(2,"红楼梦"),new storyBook(4,"水浒传"),new juvenileBook(49,"三体")}; public static void main(String[] args) { try { try { TryTest.show(); } catch (InputMismatchException e) { /*e.printStackTrace();*/ System.out.println("命令输入错误!请根据提示输入数字命令!"); TryTest.show(); } } catch (Exception e) { e.printStackTrace(); System.out.println("抱歉,您查询的图书不存在!或已被其它同学借走!"); TryTest.show(); } } private static void show() { System.out.println("输入命令查找图书:1-按照名称查找图书;2-按照序号查找图书"); int intput = sin.nextInt(); if(intput==1){ System.out.println("请输入书名:"); String iputbookname = sin.next(); for(int i=0;i<books.length;i++){ if(iputbookname.equals(books[i].bookName)){ System.out.println("恭喜您借书《"+books[i].bookName+"》成功!" ); }else { } } }else if (intput==2) { System.out.println("请输入序号:"); int iputbookid = sin.nextInt(); for(int i=0;i<books.length;i++){ if(iputbookid==books[i].bookId){ System.out.println("恭喜您借书《"+books[i].bookName +"》成功!" ); }else { } } } } }
这是正确运行程序的结果图,其中每次的输入指令处都有一个try…catch判断是否输入正确,不正确泽返回上一步骤,重新输入。