第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

圖書查詢系統(tǒng)(java異常學(xué)習(xí))

標(biāo)簽:
Java

图书类:

public class Book {
    String name = "";
    int id = -1;
    public Book (int id, String name) {
        this.id = id;
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public int getId() {
        return id;
    }
}

图书异常类:

public class BookException extends Exception {
        public BookException () {}
        public BookException (String message) {
            super (message);
        }
}

函数体:

import java.util.*;

public class Initial {
    public int getCommand () throws BookException {
        Scanner input = new Scanner (System.in);
        int command;
        try {
            command = input.nextInt(); 
        } catch (Exception e) {
            throw new BookException("错误命令异常");
        }
        if (command < 0  command >2) {
            throw new BookException("错误命令异常");
        }
        return command;
    }

    public Book searchFromName  (Book books[]) throws BookException {
        Scanner input = new Scanner (System.in);
        System.out.println ("请输入图书名称:");
        String name = input.next();
        for (int i = 0; i < books.length; i++) {
            if (books[i].getName().equals(name)) {
                return books[i];
            }
        }
        throw new BookException ("图书不存在异常");
    }

    public Book searchFromID  (Book books[]) throws BookException {
        Scanner input = new Scanner (System.in);
        System.out.println ("请输入图书序号:");
        int id;
        try {
            id = input.nextInt();
        } catch (Exception e) {
            throw new BookException("错误命令异常");
        } 
        for (int i = 0; i < books.length; i++) {
            if (books[i].getId() == id) {
                return books[i];
            }
        }
        throw new BookException ("图书不存在异常");
    }

    public void startSearch () {
        Book books [] = {new Book (1, "高数"),
                new Book (2, "线代"),
                new Book (3, "C语言"),
                new Book (4, "Java"),};
        int command;
        Book target;
        while (true) {
            System.out.println ("输入命令:0-退出;1-按照名称查找图书;2-按照序号查找图书");
            try { 
                command = getCommand(); 
            } catch (Exception e) {
                System.out.println (e.getMessage());
                continue;
            }
            switch (command) {
            case 0:
                return;
            case 1:
                try {
                    target = searchFromName (books);
                } catch (Exception e) {
                    System.out.println (e.getMessage());
                    continue;
                }
                System.out.println ("查找成功!图书序号:"+target.getId()+"\t图书名称:" +target.getName());
                break;
            case 2:
                try {
                    target = searchFromID (books);
                }  catch (Exception e) {
                    System.out.println (e.getMessage());
                    continue;
                }
                System.out.println ("查找成功!图书序号:"+target.getId()+"\t图书名称:" +target.getName());
                break;
            }   
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Initial init = new Initial ();
        System.out.println ("******欢迎进入图书查询系统!******");
        init.startSearch();
        System.out.println ("******谢谢使用,再见!******");
    }

}
點(diǎn)擊查看更多內(nèi)容
9人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消