模拟借书系统-慕课网
import java.util.InputMismatchException;
import java.util.Scanner;
/**
?* 借書書系統(tǒng)
?* @author
?*/
public class BookSystem {
?? ?String []book= {"高數(shù)","數(shù)據(jù)結構","軟件工程","數(shù)據(jù)庫","網(wǎng)絡工程"};
?? ?
?? ?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("輸入命令錯誤!請根據(jù)提示輸入數(shù)字命令!");?? ??? ??? ?
?? ??? ? ? }
?? ??? ?catch(ArrayIndexOutOfBoundsException e) {
?? ??? ??? ?System.out.println("圖書不存在");?? ??? ??? ?
?? ??? ?}
?? ??? ?catch(Exception e) {
?? ??? ??? ?System.out.println("圖書不存在");?? ??? ??? ?
?? ??? ?}
?? ? }
? ?}
}
?
運行結果:
總結
以上是生活随笔為你收集整理的模拟借书系统-慕课网的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【PC工具】在线格式转换工具,在线智能抠
- 下一篇: 求第k小元素