随风灵动
2016-07-29 16:21
import java.util.Scanner; public class Library { public String[] books = {"数学","语文","物理","英语","化学","体育"}; public static void main(String[] args){ Library lb = new Library(); lb.book(); } /** * 显示图书馆的操作命令 * 根据输入的值,执行相应的操作 */ public void book(){ System.out.println("欢迎进入图书馆借书系统"); System.out.println("1-按照名称查询图书;2-按照序号查询图书"); try { @SuppressWarnings("resource") Scanner k = new Scanner(System.in);//扫描控制台输入的值 int number = k.nextInt();//nextInt()方法会读取下一个int型标志的token.但是焦点不会移动到下一行,仍然处在这一行上。 switch (number) { case 1: find1(); break; case 2: find2(); break; default: System.out.println("请按要求输出值"); break; } } catch (Exception e) { System.out.println("请输入正确数值"); book(); } } /** * 图书馆书名查询 */ @SuppressWarnings("resource") public void find1(){ System.out.println("欢迎进入图书馆书名查询系统,请输入书名:"); try { Scanner k = new Scanner(System.in); String name = k.next(); switch(name){ case "数学": System.out.println("已查询到您所需的图书:"+name); break; case "语文": System.out.println("已查询到您所需的图书:"+name); break; case "物理": System.out.println("已查询到您所需的图书:"+name); break; case "英语": System.out.println("已查询到您所需的图书:"+name); break; case "化学": System.out.println("已查询到您所需的图书:"+name); break; case "体育": System.out.println("已查询到您所需的图书:"+name); break; default: System.out.println("非常抱歉!暂未查询出您所需的图书。"); break; } } catch (RuntimeException e) { System.out.println("您输入有误,请重新输入!"); }finally{ System.out.println("是否继续查询(Y/N);N-返回借书系统"); Scanner k1 = new Scanner(System.in); String n1 = k1.nextLine(); if(n1.equals("Y")||n1.equals("y")){ find1(); }else{ book(); } } } /** * 图书馆图书序列号查询 */ @SuppressWarnings("resource") public void find2(){ System.out.println("请输入所需查找的图书序列号:"); try { Scanner k = new Scanner(System.in); int number = k.nextInt(); if(number <= books.length-1){ System.out.println("查询出的图书为:"+books[number-1]); }else{ System.out.println("您查找的书不存在!"); } } catch (Exception e) { System.out.println("您输入有误!"); }finally{ System.out.println("是否继续查询(Y/N)"); Scanner k2 = new Scanner(System.in); String n2 = k2.nextLine(); if(n2.equals("Y")||n2.equals("y")){ find2(); }else{ book(); } } } }
bucuo
记事本中直接写Exception,程序运行时会报无法转换为Throwable
输入书名的时候try/catch,没有起到什么作用,不会报异常
Java入门第三季
409792 学习 · 4340 问题
相似问题