模拟借书系统-慕课网


import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 借书书系统
 * @author
 */
public class BookSystem {

    String []book= {"高数","数据结构","软件工程","数据库","网络工程"};
    
    public void check() throws Exception {
                System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
                
                Scanner console=new Scanner(System.in);
                int input=console.nextInt();
                
                if(input==1) {
                    System.out.println("输入图书名称:");
                    String name =console.next();
                    
                    boolean exists=false;
                    for(int i=0;i<book.length;i++) {
                        if(book[i].equals(name))  
                           {  System.out.println("Book"+name);
                          exists=true;
                           }
                    }
                   if(!exists) { throw new Exception(); }
               }else if (input==2) {
                    System.out.println("输入图书序号:");
                    int num=console.nextInt();                    
                    if(num<book.length) 
                    {  System.out.println("Book"+book[num-1]); } 
                    else
                    { throw new ArrayIndexOutOfBoundsException(); }
            }   
   }
    
    public static void main(String args[]) throws Exception {
        BookSystem bs=new BookSystem();
  while(true) {
        try {
            bs.check();
        }
        catch (InputMismatchException e) {
            System.out.println("输入命令错误!请根据提示输入数字命令!");            
           }
        catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("图书不存在");            
        }
        catch(Exception e) {
            System.out.println("图书不存在");            
        }
     }
   }
}
 


运行结果:

猜你喜欢

转载自blog.csdn.net/xxx_1_1/article/details/82657311