import?java.util.Scanner;
public?class?Library?{
????public?String[]?books?=?{"數(shù)學(xué)","語(yǔ)文","物理","英語(yǔ)","化學(xué)","體育"};
????public?static?void?main(String[]?args){
????????Library?lb?=?new?Library();
????????lb.book();
????}
????
????/**
?????*?顯示圖書館的操作命令
?????*?根據(jù)輸入的值,執(zhí)行相應(yīng)的操作
?????*/
????public?void?book(){
????????System.out.println("歡迎進(jìn)入圖書館借書系統(tǒng)");
????????System.out.println("1-按照名稱查詢圖書;2-按照序號(hào)查詢圖書");
????????try?{
????????????@SuppressWarnings("resource")
????????????Scanner?k?=?new?Scanner(System.in);//掃描控制臺(tái)輸入的值
????????????int?number?=?k.nextInt();//nextInt()方法會(huì)讀取下一個(gè)int型標(biāo)志的token.但是焦點(diǎn)不會(huì)移動(dòng)到下一行,仍然處在這一行上。
????????????switch?(number)?{
????????????case?1:
????????????????find1();
????????????????break;
????????????case?2:
????????????????find2();
????????????????break;
????????????default:
????????????????System.out.println("請(qǐng)按要求輸出值");
????????????????break;
????????????}
????????}?catch?(Exception?e)?{
????????????System.out.println("請(qǐng)輸入正確數(shù)值");
????????????book();
????????}
????}
????
????/**
?????*?圖書館書名查詢
?????*/
????@SuppressWarnings("resource")
????public?void?find1(){
????????System.out.println("歡迎進(jìn)入圖書館書名查詢系統(tǒng),請(qǐng)輸入書名:");
????????try?{
????????????Scanner?k?=?new?Scanner(System.in);
????????????String?name?=?k.next();
????????????switch(name){
????????????case?"數(shù)學(xué)":
????????????????System.out.println("已查詢到您所需的圖書:"+name);
????????????????break;
????????????case?"語(yǔ)文":
????????????????System.out.println("已查詢到您所需的圖書:"+name);
????????????????break;
????????????case?"物理":
????????????????System.out.println("已查詢到您所需的圖書:"+name);
????????????????break;
????????????case?"英語(yǔ)":
????????????????System.out.println("已查詢到您所需的圖書:"+name);
????????????????break;
????????????case?"化學(xué)":
????????????????System.out.println("已查詢到您所需的圖書:"+name);
????????????????break;
????????????case?"體育":
????????????????System.out.println("已查詢到您所需的圖書:"+name);
????????????????break;
????????????default:
????????????????System.out.println("非常抱歉!暫未查詢出您所需的圖書。");
????????????????break;
????????????}
????????}?catch?(RuntimeException?e)?{
????????????System.out.println("您輸入有誤,請(qǐng)重新輸入!");
????????}finally{
????????????System.out.println("是否繼續(xù)查詢(Y/N);N-返回借書系統(tǒng)");
????????????Scanner?k1?=?new?Scanner(System.in);
????????????String?n1?=?k1.nextLine();
????????????if(n1.equals("Y")||n1.equals("y")){
????????????????find1();
????????????}else{
????????????????book();
????????????}
????????}
????}
????
????/**
?????*?圖書館圖書序列號(hào)查詢
?????*/
????@SuppressWarnings("resource")
????public?void?find2(){
????????System.out.println("請(qǐng)輸入所需查找的圖書序列號(hào):");
????????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("是否繼續(xù)查詢(Y/N)");
????????????Scanner?k2?=?new?Scanner(System.in);
????????????String?n2?=?k2.nextLine();
????????????if(n2.equals("Y")||n2.equals("y")){
????????????????find2();
????????????}else{
????????????????book();
????????????}
????????}
????}
}
2016-07-29
bucuo
2016-08-05
記事本中直接寫Exception,程序運(yùn)行時(shí)會(huì)報(bào)無法轉(zhuǎn)換為Throwable
2016-08-05
輸入書名的時(shí)候try/catch,沒有起到什么作用,不會(huì)報(bào)異常