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

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

模擬借書系統拋出異常代碼

標簽:
Java

import java.util.;
/

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */

public class BookTry {
Scanner input = new Scanner(System.in);
private String[] bookName = {"高数","数据结构","C语言"};
public void search1() throws BookException{
System.out.println("输入图书名称:");
String k = input.nextLine();
Boolean flag = false;
for(int i =0; i<3; i++){
if(k.equals(bookName[i])){
System.out.println("book:"+bookName[i]);
flag = true;
}
}
if(!flag)
throw new BookException("图书不存在,请重新输入!!!");
}
public void search2() throws BookException{
System.out.println("输入图书序号:");
try{
Scanner inp = new Scanner(System.in);
int k = inp.nextInt();
if(k>=0&&k<3)
System.out.println("book:"+bookName[k]);
else
throw new BookException("图书不存在,请重新输入!!!");
}catch(InputMismatchException ime){
System.out.println("命令输入错误! 请重新输入!");
search2();
}
}
public void start(){
Scanner in = new Scanner(System.in);
System.out.print("输入命令:1-按名称查找图书;2-按序号查找图书");
try{
int j = in.nextInt();
if(j == 1)
search1();
else if(j == 2)
search2();
}catch(InputMismatchException ime){
System.out.println("命令输入错误! 请重新输入!");
start();
}catch(BookException be){
System.out.println(be.getMessage());
start();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BookTry bookTry = new BookTry();
try {
bookTry.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

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

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

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

評論

作者其他優質文章

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

100積分直接送

付費專欄免費學

大額優惠券免費領

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

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

幫助反饋 APP下載

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

公眾號

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

舉報

0/150
提交
取消