import java.util.Scanner;
public class SimulateLibrary {
public String[][] Books = {{"1","十万个为什么"},{"2","Java语言程序设计"},{"3","JavaScript权威指南"},{"4","网页设计"}};
public static void main(String[] args) {
System.out.println("请输入你要搜索的方式:");
System.out.println("1. 按照图书序号搜索");
System.out.println("2. 按照书名搜索");
SimulateLibrary SL = new SimulateLibrary();
Scanner sc = new Scanner(System.in);
int index = sc.nextInt();
if(index == 1) {
System.out.println("请输入图书序号");
int BookIndex = sc.nextInt();
SL.SearchByIndex(BookIndex);
}else if(index == 2) {
System.out.println("请输入图书全名");
String BookName = sc.next();
SL.SearchByBookName(BookName);
}else {
System.out.println("命令输入有误,请根据提示输入数字命令!");
}
sc.close();
}
public void SearchByIndex(int BookIndex) {
BookIndex--;
try {
System.out.println("找到书本《"+Books[BookIndex][1]+"》");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("图书不存在");
}catch(Exception e) {
System.out.println("错误命令异常,请重新输入");
}
}
public void SearchByBookName(String BookName) {
String findBook = null;
int BookNum = -1;
for(int i=0;i<Books.length;i++) {
if(Books[i][1].equals(BookName)) {
findBook = Books[i][1];
BookNum = i+1;
break;
}
}
if(BookNum!=-1) {
System.out.println("找到书本《"+findBook+"》,图书序号"+BookNum);
}else {
System.out.println("图书不存在");
}
}
}
点击查看更多内容
4人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦