import java.util.Scanner; public class Book { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("欢迎来到书库"); //定义图书数组 final int [] booknum ={1,2,3,4,5}; final String [] bookname={"c++","java","计算机基础","汇编语言","算法"}; //展示编号和图书 for(int i=0;i<5;i++) System.out.println(booknum[i]+"\t"+bookname[i]+"\t"); //选择方式 System.out.println("请输入查询方式的编号!1.按照编号查询 2.按照书名查询"); try{ String number=input.next(); //进行选择 switch(number){ case "1": //1编号查询 System.out.println("请输入编号"); nums(bookname); break; case "2": //2书名查询 System.out.println("请输入书名"); names(bookname); break; default: //输入错误 System.out.println("输入错误,请输入正确数字"); } }catch(Exception aaa){ System.out.println(aaa.getMessage()); } } //编号查询方法 public static void nums(String [] bookname){ Scanner in=new Scanner(System.in); try{ int a=in.nextInt(); System.out.println("给你"+bookname[a-1]); }catch(Exception a){ System.out.println("无此书"); } } //名字查询方法 public static void names(String [] bookname) throws Exception{ Scanner in=new Scanner(System.in); String b=in.nextLine(); for(int i=0;i<bookname.length;i++){ if (b.equals(bookname[i])) System.out.println("给你"+bookname[i]); }throw new Exception("暂无此书"); } }
// 名字查询方法 public static void names(String[] bookname) throws Exception { Scanner in = new Scanner(System.in); String b = in.nextLine(); for (int i = 0; i < bookname.length; i++) { if (b.equals(bookname[i])) { System.out.println("给你" + bookname[i]); return; } } throw new Exception("暂无此书"); }
改成这样就行了,求采纳!