交作業(yè)。。
package practise;
import java.util.InputMismatchException;
import java.util.Scanner;
public class BorrowBook {
????public static void main(String[] args) {
????????BorrowBook borrowBook=new BorrowBook();
????????borrowBook.borrow();
????}
????public void borrow() {
????????System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號(hào)查找圖書");
????????Scanner sc1=new Scanner(System.in);
????????try {
????????? ??int num=sc1.nextInt();
????????????if(num==1) {
????????????bookname();
????????????}else if(num==2) {
????????????booknum();
????????????}
????????????sc1.close();
????????}catch(Exception e) {
????????????System.out.println("命令輸入錯(cuò)誤,請(qǐng)根據(jù)提示輸入數(shù)字命令!");
????????????borrow();
????????}
????}
????public void bookname(){
????????String[] books= {"高數(shù)","數(shù)據(jù)結(jié)構(gòu)"};
????????System.out.println("輸入圖書名稱:");
????????Scanner sc1=new Scanner(System.in);
????????String str1=sc1.nextLine();
????????try {
????????????for(int i=0;i<=books.length;i++) {
????????????????if(books[i].equals(str1)) {
????????????????????System.out.println("book:"+str1);
????????????????????break;
????????????????}
????????????}
????????}catch(Exception e) {
????????????System.out.println("圖書不存在!");
????????????borrow();
????????}
????????sc1.close();
????}
????public void booknum() {
????????String[] books= {"高數(shù)","數(shù)據(jù)結(jié)構(gòu)"};
????????System.out.println("輸入圖書序號(hào):");
????????Scanner sc1=new Scanner(System.in);
????????try {
????????????int num=sc1.nextInt();
????????????System.out.println("book:"+books[num-1]);
????????}catch(InputMismatchException e) {
????????????System.out.println("命令輸入錯(cuò)誤,請(qǐng)根據(jù)提示輸入數(shù)字命令!");
????????????booknum();
????????}catch(Exception e) {
????????????System.out.println("圖書不存在!");
????????????borrow();
????????}
????????sc1.close();
}
}
2019-07-22
你只是捕獲異常 卻沒(méi)有拋出異常