继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

模拟借书系统(ps:参考大神的编的)

心中的愤怒就像龙咆哮
关注TA
已关注
手记 7
粉丝 9
获赞 199
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;
        }
    }

}

图片描述

图片描述

打开App,阅读手记
13人推荐
发表评论
随时随地看视频慕课网APP

热门评论

你好,我想问一下,input 函数 中,是怎样判断然后让返回值等于-1 的?   

如果我输入5,input函数中异常是怎样运行的呢?

for(String BK : bk.book)
语法不是直接for(String BK : bk)?



确定循环次数用for

不确定用while

查看全部评论