package com.imooc; import java.util.Scanner; public class TestException { public int read() { int choose = 0; try { System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书"); Scanner in = new Scanner(System.in); choose = in.nextInt(); } catch (Exception e) { System.out.println("命令输入错误!请根据提示输入数字命令!"); } return choose; } public int readBookNum() { int num = 0; try { System.out.println("输入图书序号:"); Scanner in = new Scanner(System.in); num = in.nextInt(); } catch (Exception e) { System.out.println("命令输入错误!请根据提示输入数字命令!"); } return num; } public static void main(String[] args) { // TODO Auto-generated method stub String[] books = new String[] { "算法导论", "数据结构", "高数" }; TestException test1 = new TestException(); int num1; outer: while (true) { num1 = test1.read(); if (num1 == 1) { boolean flag1 = false; Scanner in = new Scanner(System.in); System.out.println("输入图书名称:"); String name = in.nextLine(); inner: for (int j = 0; j < books.length; j++) { if (books[j].equals(name)) { flag1 = true; break inner; } } if (flag1) { System.out.println("book:" + name); break outer; } else { System.out.println("图书不存在!"); } } else if (num1 == 2) { boolean flag2 = false; int num = test1.readBookNum(); inner: for (int i = 0; i < books.length; i++) { if (num == i) { flag2 = true; break inner; } } if (flag2) { System.out.println("book:" + books[num]); break outer; } else { System.out.println("图书不存在!"); } } } } }
你是大哥