借书系统,第二步输入书籍名称老是错误,大神求解

来源:1-9 经验总结

weixin_慕粉8535037

2019-02-14 00:50


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();

}

}

}


写回答 关注

3回答

  • 亦菲大魔王
    2019-03-14 13:17:16

    String inputName = input.next();

    改成

    String inputName = input.nextLine();试试看

  • 慕粉3593996
    2019-02-16 23:38:31

    System.out.println("您的书籍为:" + bookName[i]);加break;跳出循环。else写在循环外试试 


  • 这鱼又溺水了
    2019-02-14 18:30:13

    具体是什么类型的错误?是你所输入的书名找不到还是其他?

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题