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();
}
}
}
String inputName = input.next();
改成
String inputName = input.nextLine();试试看
System.out.println("您的书籍为:" + bookName[i]);加break;跳出循环。else写在循环外试试
具体是什么类型的错误?是你所输入的书名找不到还是其他?