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

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

作業提交 。

InputException.java

package com.imooc.test;

public class InputException extends Exception {

???? public InputException() {

???????? super();

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

???? }

???? public InputException(String message) {

???????? super(message);

???????? System.out.println(message);

???? }

}

BookBorrow.java

package com.imooc.test;


import java.util.Arrays;

import java.util.InputMismatchException;

import java.util.Scanner;


public class BookBorrow {

???? private static String[] books = { "高數", "積分變換", "線性代數", "馬克思哲學" };

????

???? public static void main(String[] args) {

???????? // TODO Auto-generated method stub

???????? BookBorrow bkb = new BookBorrow();

???????? bkb.borrow();

???? }

????

???? public void borrowByName(Scanner input) throws InputException {

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

???????? String name = input.next();

???????? if (-1 == Arrays.binarySearch(books, name)) {

???????? ???? System.out.println("圖書不存在!");

? ? ? ? ? ? ?throw new InputException();

???????? } else {

???????????? System.out.println("book: " + name);

???????? }

???? }

????

???? public void borrowByNumber(Scanner input) throws InputException {

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

???????? int num = input.nextInt();

???????? if (num < 1 || num > books.length) {

???????? ???? System.out.println("圖書不存在!");

? ? ? ? ? ? ? throw new InputException();

???????? } else {

???????? ???? System.out.println("book: " + books[num - 1]);

???????? }

???? }

????

???? public void borrow() {

???????? Scanner input = new Scanner(System.in);

???????? System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");

???????? try {

???????????? switch (input.nextInt()) {

???????????????? case 1:

???????????????? ???? borrowByName(input);

???????????????? ???? break;

???????????????? case 2:

???????????????????? borrowByNumber(input);

???????????????????? break;

???????????????? default:

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

???????????????????? borrow();

???????????????????? break;

???????????? }

???????? } catch(InputException e) {

????????

???????? }catch (InputMismatchException e) {

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

???????? } catch (Exception e) {

???????????? // TODO: handle exception

???????????? e.printStackTrace();

???????? } finally {

???????????? borrow();

???????????? input.close();

???????? }

???? }

}


正在回答

2 回答

程序存在一些問題:1. 根據要求查詢的第二步 ? 根據書名或序號查詢若輸入數據格式不正確沒有一個明確的提示錯誤的類型以及錯誤后應繼續執行主程序的操作。2. 要求根據書名或圖書序號查詢查詢,圖書和序號應存在唯一對應關系,所以最好把數據存儲在map集合中。

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

路過請多指教

恍恍惚惚朦朦朧

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

舉報

0/150
提交
取消
Java入門第三季
  • 參與學習       409767    人
  • 解答問題       4543    個

Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧

進入課程

作業提交 。

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

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

幫助反饋 APP下載

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

公眾號

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