package com.imooc.book; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Book { public static void main(String[] args) { List<String> bookList=new ArrayList(); bookList.add("西游记"); bookList.add("红楼梦"); bookList.add("水浒传"); bookList.add("三国演义"); System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书"); int order = 0; Scanner scanner=new Scanner(System.in); try { order = scanner.nextInt(); } catch (Exception e) { System.out.println("命令输入错误!请根据提示输入数字命令"); } switch (order) { case 1: System.out.println("输入图书名称:"); String bookName; bookName=scanner.next(); try{for (int i=0;i<=bookList.size();i++) { String book=bookList.get(i); if(bookName.equals(book)){ System.out.println("book:"+book); break; } }}catch (Exception e){ System.out.println("图书不存在!");} break; case 2: System.out.println("输入图书序号:"); int num=0; try { num = scanner.nextInt(); } catch (Exception e) { System.out.println("命令输入错误!请根据提示输入数字命令");break; } try{ System.out.println("book:"+bookList.get(num-1)); }catch (Exception e){ System.out.println("图书不存在!"); } break; default: System.out.println("抱歉,没有此项命令!"); } } }
这个写得还可以