手记

图书借阅,欢迎交流、

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 < arr.length;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<arr.length){
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;
}
}
}
}

5人推荐
随时随地看视频
慕课网APP

热门评论

存在错误。应该把

if (i == arr.length)

throw new Exception();

放到for循环外。

errException要怎么定义呢。

查看全部评论