问答详情
源自:1-9 经验总结

交作业(图书查询)

public class Initial {

public static void main(String[] args) {

String[] library = {"xml","ssm","数据结构与算法"};

int n = 0;

while (true) {

try {

Scanner scanner = new Scanner(System.in);

String tipString = "图书不存在!";

System.out.println("输入命令:1-按书名查找 2-按编号查找");

n = scanner.nextInt();

if (n==1) {

try {

System.out.println("请输入书名:");

String name = scanner.next();

for (int i = 0; i < library.length; i++) {

if (name.equals(library[i])) {

tipString = "book:"+name;

break;

}

}

} catch (Exception e) {

continue;

} finally {

System.out.println(tipString);

}

}else if (n==2) {

System.out.println("请输入编号:");

try {

int num = scanner.nextInt();

tipString = "book:"+library[num];

System.out.println(tipString);

}catch (Exception e) {

System.out.println("请输入数值");

continue;

}

} catch (Exception e) {

System.out.println("根据提示输入正确命令!");

continue;

}

}

}


提问者:Handsun007 2019-01-12 16:17

个回答

  • 慕慕9136061
    2019-01-12 17:41:29

    当系统要求"输入命令:1-按书名查找 2-按编号查找"的时候

    输入3 不会提示""根据提示输入正确命令!"

    在输入编号时 若输入字符,会直接退回命令选择而不是让我重新输入编号