借书系统,第二步输入书籍名称老是错误,大神求解
import java.util.Scanner;
public class TakeBook {
public static void main(String[] args) {
TakeBook test = new TakeBook();
test.select();
}
public void select() {
String[] bookName = { "Java", "python", "C++" };
System.out.println("请输入对应的命令查找书籍:\n1--按编号查找书籍\n2--按照书籍名称查找书籍");
Scanner input = new Scanner(System.in);
try {
int order = input.nextInt();
if (order != 1 && order != 2) {
throw new Exception();
} else {
if (order == 1) {
System.out.println("请输入书籍编号:");
try {
int bookNum = input.nextInt();
if (bookNum < 1 || bookNum > bookName.length) {
throw new Exception();
} else {
System.out.println("您的书籍为:" + bookName[bookNum - 1]);
}
} catch (Exception e) {
System.out.println("请输入正确的编号!\n");
throw new Exception();
}
}
}
if (order == 2) {
System.out.println("请输入书籍名称:");
try {
String inputName = input.next();
for (int i = 0; i <= bookName.length - 1; i++) {
if (inputName.equals(bookName[i])) {
System.out.println("您的书籍为:" + bookName[i]);
} else {
throw new Exception();
}
}
} catch (Exception e) {
System.out.println("您输入的书籍不存在,请重新输入\n");
throw new Exception();
}
}
} catch (Exception e) {
System.out.println("---请按照提示输入正确信息---");
select();
}
}
}