package imooc;
import java.util.Scanner;
public class Book {
public static Scanner inputScanner = new Scanner(System.in);
public static void main(String[] args) {
String[] bookStrings = { "数据结构", "c语言", "操作系统", "英语" };
String book;
while (true) {
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
try {
int command = inputCommand();
switch (command) {
case 1:
book = getbookByname(bookStrings);
System.out.println("book:" + book);
break;
case 2:
book = getBookById(bookStrings);
System.out.println("book:" + book);
break;
case -1:
System.out.println("命令输入错误,请根据提示输入数字命令!");
continue;
default:
System.out.println("命令输入错误!");
continue;
}
break;
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
continue;// continue只能在循环里使用,若输入错误则再重新循环指令进行输入
}
}
}
// 按名称查找
private static String getbookByname(String[] books) throws Exception {
System.out.println("请输入图书名称");
String nameString = inputScanner.next();
for (int i = 0; i < books.length; i++) {
if (nameString.equals(books[i]))
return nameString;
}
throw new Exception("图书不存在");
}
// 按序号查找
private static String getBookById(String[] books) throws Exception {
while (true) {
System.out.println("请输入图书序号");
try {
int index = inputScanner.nextInt();
if (index == -1) {
System.out.println("命令行输入错误!请根据提示输入数字命令");
}
String bookString=books[index];
return bookString;
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
Exception bookNotExists =new Exception("图书不存在");
bookNotExists.initCause(e);
throw bookNotExists;
}
}
}
// 获得外键输入的选择
private static int inputCommand() {
int command;
try {
command = inputScanner.nextInt();
return command;
} catch (Exception e) {
// TODO: handle exception
inputScanner = new Scanner(System.in);
return -1;
}
}
}
热门评论
运行后发现有bug,输出图书那里图书不存在也可以输出。
可以多些解释,而且格式有点乱,switch语句部分有余
小白,看的有点儿迷,英语不好看起来真难受