package com.imooc.test;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Exercise {
private static String[] books = {"Java编程思想", "C语言程序设计", "微积分", "数据结构", "计算机科学导论"};
public static void main(String[] args) {
Exercise exercise = new Exercise();
exercise.method();
}
public void method() {
Scanner in = new Scanner(System.in);
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
try {
int index = in.nextInt();
switch (index) {
case 1 :
System.out.println("输入图书名称:");
String name = in.next();
for ( String s : Exercise.books) {
if ( s.equals(name) ) {
System.out.println("book:"+name);
return;
}
}
System.out.println("图书不存在!");
method();
break;
case 2:
System.out.println("输入图书序号:");
try {
int num = in.nextInt();
for ( String s : Exercise.books) {
if ( s.equals(Exercise.books[num-1])) {
System.out.println("book:"+Exercise.books[num-1]);
return;
}
}
System.out.println("图书不存在!");
method();
} catch ( Exception e ) {
System.out.println("命令输入错误!请根据提示输入数字命令!");
method();
}
break;
}
} catch ( InputMismatchException input ) {
System.out.println("命令输入错误!请根据提示输入数字命令!");
method();
}
}
}