亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

模擬借書系統(ps:參考大神的編的)

標簽:
Java
package com.project3;

public class Book {
    String [] book ={"语文","数学","英语","生物","物理","化学"};
}
package com.project3;
import java.util.Scanner;
public class bookSystem {
    private static Scanner in = new Scanner(System.in);
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("欢迎来到借书系统O~O");
        System.out.println("图书列表:");
        System.out.println("序号"+"\t"+"书名");
        Book bk = new Book();
        int i = 0;
        for(String BK : bk.book){
            System.out.println(i+"\t"+BK);
            i++;
        }
        for(;;){
            try{
                System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
                int input = Input();
                switch(input){
                    case -1: System.out.println("命令输入错误!请输入int数据类型!");
                            continue;
                    case 1: String name = getbyname(bk.book);
                            System.out.println("book:"+name);
                            in.close();
                            break;
                    case 2: String name2 = getbynum(bk.book);
                            System.out.println("book:"+name2);
                            in.close();
                            break;
                    default: System.out.println("命令输入错误!请按照提示输入命令!");
                            continue;
                }
                break;
            }catch(Exception e){
                System.out.println(e.getMessage());
                continue;
            }
        }
    }
    public static String getbyname(String[] book) throws Exception{
        for(;;){
            try{
                System.out.print("请输入书名:");
                String input1 = in.next();
                for(String b:book){
                    if(input1.equals(b)){
                        return b;
                    }
                }
                throw new Exception();
            }catch(Exception e){
                System.out.println("图书不存在!");
            }
        }
    }
    public static String getbynum(String[] book)throws Exception{
        for(;;){
            try{
                System.out.print("请输入序号:");
                int input2 = Input();
                if(input2 == -1){
                    System.out.println("命令输入错误!请输入int数据类型!");
                    continue;
                }else if(0<=input2&&input2<book.length){
                    return book[input2];
                }
                throw new Exception(); 
            }catch(Exception e){
                System.out.println("图书不存在!");
            }
        }
    }
    public static int Input(){
        try {
            int input = in.nextInt();
            return input;
        } catch (Exception e) {
            // TODO: handle exception
            in = new Scanner(System.in);
            return -1;
        }
    }

}

图片描述

图片描述

點擊查看更多內容
13人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

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

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消