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

实现代码如下

复制粘贴格式化,我还没调试,如果有错误可以回复分享一下。

package com.booksearch;


import java.util.*;


public class BookSearch {

public static void main(String[] args) {

String[] books = { "数据结构", "高数", "论语" };

boolean flag = true;

while (flag) {

try {

System.out.println("输入命令:1.按照名称查图书;2.按照序号查图书");

Scanner input = new Scanner(System.in);

int cmdnum = input.nextInt();

if (cmdnum == 1) {

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

String name = input.next();

for (String book : books) {

if (book.equals(name)) {

System.out.println("book:" + book);

flag = false;

break;

}

}

if (flag) {

System.out.println("图书不存在");

continue;

}

} else if (cmdnum == 2) {

System.out.println("输入图书序号:");

int id = input.nextInt();

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

if (id > 0 && id <= books.length) {

System.out.println("book:" + books[id - 1]);

flag = false;

break;

}

}

if (flag) {

System.out.println("图书不存在");

continue;

}

}

} catch (Exception e) {

System.out.println("命令输入错误!请输入数字命令!");

continue;

}

}

}

}


提问者:神影天初 2018-08-12 13:48

个回答

  • omoide
    2018-08-12 15:08:58

    try catch没用吧。。