圖書借閱,歡迎交流、

duanhao發表於2021-09-09

package module;
import java.util.InputMismatchException;
import java.util.Scanner;
public class tryCatch {
public static void main(String[] args){
// TODO Auto-generated method stub
String arr[]={"高等數學","資料結構","演算法分析","作業系統","計算機大全","水滸傳","三國演義","西遊記","紅樓夢","百科全書"};
System.out.println("請輸入命令:1按名稱查圖書 2按序號查圖書");
Scanner scanner=new Scanner(System.in);
while(true){
try{
int n=scanner.nextInt();
if(n==1){
System.out.print("請輸入圖書名稱"+"n");
Scanner scanner1=new Scanner(System.in);
String st=scanner1.nextLine();
for (int i = 0; i if(arr[i].equals(st)){
System.out.println("此書是第"+i+"本書");
break;
}
if(i==arr.length)
throw new errException();
}
scanner1.close();
break;
}
if(n==2){
System.out.println("請輸入圖書序號");
int x=scanner.nextInt();
if(x System.out.println("書名為:"+arr[x]);
break;
}
throw new ArrayIndexOutOfBoundsException();
}
throw new InputMismatchException();
}catch (InputMismatchException e) {
System.out.println("輸入錯誤請重新輸入:1按名稱查圖書 2按序號查圖書");
scanner=new Scanner(System.in);
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("圖書不存在");
scanner.close();
break;
}catch (errException e) {
System.out.println("圖書不存在");
scanner.close();
break;
}
}
}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1343/viewspace-2807998/,如需轉載,請註明出處,否則將追究法律責任。

相關文章