自己写的,仅供参考
package booktest;
import java.util.Scanner;
public class book {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean n = true;
String[] bookname = new String[] {"高数","数据结构"};
while(n)
{
try {
Scanner input = new Scanner(System.in);
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
int number = input.nextInt();
if(number == 1){
System.out.println("输入图书名称:");
Scanner input1 = new Scanner(System.in);
String a = input1.nextLine();
for(String b:bookname) {
if(b.contains(a)) {
System.out.println("book:"+ a);
break;
}
else {
System.out.println("图书不存在!");
break;
}
}
}
else if(number ==2){
System.out.println("输入图书序号:");
int c = input.nextInt();
if(c == 0) {
System.out.println("book:" + bookname[c]);
break;
}
else if(c == 1) {
System.out.println("book:" + bookname[1]);
break;
}
else {
System.out.println("图书不存在!");
}
}
}catch(Exception e){
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
}
}
}
}