借書系統(tǒng),第二步輸入書籍名稱老是錯誤,大神求解
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();
}
}
}
2019-03-14
String inputName = input.next();
改成
String inputName = input.nextLine();試試看
2019-02-16
System.out.println("您的書籍為:" + bookName[i]);加break;跳出循環(huán)。else寫在循環(huán)外試試?
2019-02-14
具體是什么類型的錯誤?是你所輸入的書名找不到還是其他?