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

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

作業—模擬借書系統

package com.imooc;


import java.util.Scanner;


public class BookLending {


// 定義圖書數組

static Books[] booksToInquery = { new Books(1, "Java開發實戰修煉"),

new Books(2, "JavaWeb開發實戰經典"), new Books(3, "瘋狂android講義"),

new Books(4, "android權威指南"), new Books(5, "HeadFirst設計模式") };

static Scanner sc = new Scanner(System.in);


// 主查詢方法

public static void inquery() throws Exception {


System.out.println("歡迎登錄模擬借書系統!"); // 進入系統

System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書"); // 選擇查詢類型

try {

int input = inputCommand(); // 將控制臺輸入的數字、字符、字符串轉化1,2,-1

switch (input) {

case 1: // 輸入1,則按照書名查詢

System.out.println("輸入圖書名稱:");

String bookName = sc.next();

checkName(bookName);

break;

case 2: // 輸入2,則按照編號查詢

checkSerial();

break;

case -1: // 輸入字符或字符串,則提示錯誤,重新輸入

System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");

inquery();

default: // 輸入其他類型,提示錯誤

System.out.println("命令輸入錯誤!");

inquery();

}

} catch (Exception e) {

// 捕獲”圖書不存在異常“時,要求重新輸入命令

System.out.println(e.getMessage());

inquery();

}


}


// 從控制臺輸入命令,用于選擇查詢類型以及選擇查詢編號

public static int inputCommand() {

try {

int input = sc.nextInt();

return input;

} catch (Exception e) {

// 若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出"錯誤命令異常"

sc = new Scanner(System.in);

return -1;

}

}


// 按照書名查找圖書

public static void checkName(String st) throws Exception {

for (int i = 0; i < booksToInquery.length; i++) {

// 如輸入書名正確,則返回相應位置圖書

if (st.equals(booksToInquery[i].getName())) {

System.out.println("序號: " + booksToInquery[i].serial + "\t"

+ "書名: " + booksToInquery[i].name);

System.exit(0);

}

}

throw new Exception("圖書不存在哦!");

}


// 按照編號查找圖書

public static void checkSerial() throws Exception {

System.out.println("輸入圖書序號:");

try {

int bookSerial = inputCommand(); // 將輸入的序號做一個轉化,輸入數字則返回數字,輸入字符和字符串則返回-1


// 如果輸入字符及字符型,則提示錯誤,重新輸入

if (bookSerial == -1) {

System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");

checkSerial();

}

// 如輸入序號正確,則返回相應位置圖書

for (int i = 0; i < booksToInquery.length; i++) {

if (bookSerial == booksToInquery[i].serial) {

System.out.println("序號: " + booksToInquery[i].serial + "\t"

+ "書名: " + booksToInquery[i].name);

System.exit(0);

}

}

} catch (ArrayIndexOutOfBoundsException e) {

// 輸入的序號不存在(引發"數組下標越界異常"),則拋出"圖書不存在異常"

Exception bookNotExists = new Exception("圖書不存在哦!");

bookNotExists.initCause(e);

throw bookNotExists;

}


}


public static void main(String[] args) throws Exception {


inquery();

}


}








package com.imooc;


public class Books {

public String name;

public int serial;


public Books(int serial, String name) {

this.name = name;

this.serial = serial;


}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}


public int getSerial() {

return serial;

}


public void setSerial(int serial) {

this.serial = serial;

}


}


正在回答

1 回答

!!!!

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

作業—模擬借書系統

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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