package com.jsdx; import java.util.Scanner; import java.util.Arrays; public class RentBook { private static String[] bookName = {"悟空传","斗罗大陆","斗破苍穹","择天记","遮天"}; public RentBook() { } public static void main(String[] args) { RentBook rentBook = new RentBook(); rentBook.select(); } public void bookNumber() { System.out.println("输入图书序号:"); @SuppressWarnings("resource") Scanner inNumber = new Scanner(System.in); if(inNumber.hasNextInt()) { int number = inNumber.nextInt(); if(number > 0 && number <= bookName.length) { System.out.println("图书信息:" + bookName[number-1]); } else { try { System.out.println("没有该序号图书!"); select(); } catch (Exception e) { e.printStackTrace(); } } } else { try { System.out.println("输入错误,请输入数字!"); bookNumber(); } catch (Exception e) { e.printStackTrace(); } } } public void inName() { System.out.println("输入图书名称:"); @SuppressWarnings("resource") String name = new Scanner(System.in).nextLine(); boolean flag = Arrays.asList(bookName).contains(name); if(flag) { System.out.println("图书信息:" + name); } else try { System.out.println("没有该名称图书!"); select(); } catch (Exception e1) { e1.printStackTrace(); } } public void select() { System.out.println("输入命令:1-按书名查找图书; 2-按序号查找图书"); @SuppressWarnings("resource") Scanner inCammond = new Scanner(System.in); try { if(inCammond.hasNextInt()) { int cammond = inCammond.nextInt(); if(cammond == 1) { inName(); } else if (cammond == 2) { bookNumber(); } else { throw new CammondException(); } } else { throw new CammondException(); } }catch (CammondException e) { System.out.println("命令无效,请按提示输入命令!!!"); select(); } } }
//CammondException时一般的自定义类型
我其实都不懂什么是异常