ITPub博客

首页 > IT职业 > IT生活 > 图书借阅,欢迎交流、

图书借阅,欢迎交流、

IT生活 作者:duanhao 时间:2021-09-09 09:05:26 0 删除 编辑

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博客 ” ,链接:https://blog.itpub.net/1343/viewspace-2807998/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-09-06

  • 博文量
    739
  • 访问量
    25599