继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

图书借阅,欢迎交流、

花花_公纸
关注TA
已关注
手记 1
粉丝 1
获赞 5

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;
}
}
}
}

打开App,阅读手记
5人推荐
发表评论
随时随地看视频慕课网APP

热门评论

存在错误。应该把

if (i == arr.length)

throw new Exception();

放到for循环外。

errException要怎么定义呢。

查看全部评论