1 package com.company.imooc_chapter_3;
2
3 import java.util.Scanner;
4
5 /**
6 * Created by junius on 2016/12/13.
7 */
8 public class TestSearch {
9 private static Scanner console = new Scanner(System.in);
10
11 public static void main(String[] args){
12 String[] books = { "C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概" };
13 while(true){
14 System.out.println("输入命令:1-按照名称查找图书,2-按照序号查找图书");
15 String book;
16 try {
17 int command =inputCommand();
18 switch (command){
19 case 1:
20 book = getBookByName(books);
21 System.out.println("book:"+book);
22 break;
23 case 2:
24 book = getBookByNo(books);
25 System.out.println("book" + book);
26 break;
27 case -1:
28 System.out.println("输入的命令有误,请重新输入!");
29 continue;
30 default:
31 System.out.println("请输入提示中的数字");
32 continue;
33 }
34 break;
35 } catch (Exception e) {
36 System.out.println(e.getMessage());
37
38 }
39 }
40 }
41
42 private static int inputCommand() {
43 int command;
44 try {
45 command = console.nextInt();
46 return command;
47 } catch (Exception e) {
48 console = new Scanner(System.in);
49 return -1;
50 }
51 }
52
53 private static String getBookByNo(String[] books) throws Exception{
54 while(true){
55 System.out.println("请输入图书序号:");
56 try {
57 int index =inputCommand();
58 if(index==-1){
59 System.out.println("输入的序号错误请重新输入!");
60 continue;
61 }
62 String book = books[index];
63 return book;
64 } catch (ArrayIndexOutOfBoundsException ex) {
65 Exception bookIsntExist = new Exception("图书不存在");
66 bookIsntExist.initCause(ex);
67 throw bookIsntExist;
68 }
69 }
70 }
71
72 private static String getBookByName(String[] books) throws Exception{
73 System.out.println("请输入书名");
74 String name = console.next();
75 for(String na:books){
76 if(name.equals(na)){
77 return na;
78 }
79 }
80 throw new Exception("图书不存在!");
81 }
82 }
1 package com.company.imooc_chapter_3;
2
3 import java.util.Scanner;
4
5 /**
6 * Created by junius on 2016/12/13.
7 */
8 public class TestSearch {
9 private static Scanner console = new Scanner(System.in);
10
11 public static void main(String[] args){
12 String[] books = { "C语言", "数据结构", "汇编语言", "高数", "大学语文", "毛概" };
13 while(true){
14 System.out.println("输入命令:1-按照名称查找图书,2-按照序号查找图书");
15 String book;
16 try {
17 int command =inputCommand();
18 switch (command){
19 case 1:
20 book = getBookByName(books);
21 System.out.println("book:"+book);
22 break;
23 case 2:
24 book = getBookByNo(books);
25 System.out.println("book" + book);
26 break;
27 case -1:
28 System.out.println("输入的命令有误,请重新输入!");
29 continue;
30 default:
31 System.out.println("请输入提示中的数字");
32 continue;
33 }
34 break;
35 } catch (Exception e) {
36 System.out.println(e.getMessage());
37
38 }
39 }
40 }
41
42 private static int inputCommand() {
43 int command;
44 try {
45 command = console.nextInt();
46 return command;
47 } catch (Exception e) {
48 console = new Scanner(System.in);
49 return -1;
50 }
51 }
52
53 private static String getBookByNo(String[] books) throws Exception{
54 while(true){
55 System.out.println("请输入图书序号:");
56 try {
57 int index =inputCommand();
58 if(index==-1){
59 System.out.println("输入的序号错误请重新输入!");
60 continue;
61 }
62 String book = books[index];
63 return book;
64 } catch (ArrayIndexOutOfBoundsException ex) {
65 Exception bookIsntExist = new Exception("图书不存在");
66 bookIsntExist.initCause(ex);
67 throw bookIsntExist;
68 }
69 }
70 }
71
72 private static String getBookByName(String[] books) throws Exception{
73 System.out.println("请输入书名");
74 String name = console.next();
75 for(String na:books){
76 if(name.equals(na)){
77 return na;
78 }
79 }
80 throw new Exception("图书不存在!");
81 }
82 }