作業(yè)求大神幫忙看看我錯(cuò)在哪了,掃描儀關(guān)不了
package try_catch;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TryCatchPractice {
public static void main(String[]args){
TryCatchPractice trycat=new TryCatchPractice();
trycat.InputClassException();
}
public void InputClassException(){
try{
Scanner in=new Scanner(System.in);
String[] books={"語(yǔ)文","數(shù)學(xué)","英語(yǔ)"};
System.out.println("歡迎光臨本圖書管理系統(tǒng)");
System.out.println("1.書名查詢。2.序號(hào)查詢");
int a=in.nextInt();
switch(a){
case 1: //查找圖書
System.out.println("請(qǐng)輸入您要查詢的圖書:");
String b=in.next();
try{
for(int i=0;i<books.length;i++){
if(books[i].equals(b)){//如果查詢的書名不為空,輸出書本信息
System.out.println("查詢成功");
}else{//如果查詢?yōu)榭?,拋出異?/p>
throw new NullPointerException("圖書不存在");
}
}
}catch(NullPointerException e){
e.printStackTrace();//捕捉到異常后輸出異常位置
System.out.println("請(qǐng)重新輸入:");
InputClassException();//重新進(jìn)入系統(tǒng)
}
break;
case 2://查找序號(hào)
System.out.println("請(qǐng)輸入您要借閱的圖書序號(hào):");
int i=in.nextInt();
try{
if(i<books.length&&i>=0){//如果查詢的序號(hào)不越界,輸出書本信息
System.out.println("查詢成功");
System.out.println("您查詢的是"+books[i]);
}else{//越界則拋出數(shù)組越界異常
throw new ArrayIndexOutOfBoundsException("序號(hào)錯(cuò)誤");
}
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();//捕捉到異常后輸出異常位置
System.out.println("請(qǐng)重新輸入:");
InputClassException();//重新進(jìn)入系統(tǒng)
}
break;
}
in.close();
}catch(InputMismatchException e){
System.out.println("請(qǐng)輸入整數(shù)1或2");
e.printStackTrace();
System.out.println("請(qǐng)重新輸入:");
InputClassException();
}
}
}
2019-05-19
111