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

交作业(1-9图书查询系统)

package book_retrieval_system;

import java.util.Scanner;


public class RetrievalSystem {

String[] books = {"Head First Java","Java数据结构","Java从入门到精通"};

public static void main(String[] args) {

// TODO Auto-generated method stub

RetrievalSystem find = new RetrievalSystem();

find.books();

}


public void books(){

try {

System.out.println("请选择查找方式:1、以书名查找。2、以图书序号查找。");

Scanner inputWays = new Scanner (System.in);

int way = inputWays.nextInt();

if(way==1||way==2) {

ways(way);

}else {

System.out.println("命令输入错误,请输入1或2");

books();

}

}catch(Exception ex) {

System.out.println("命令输入错误,请输入1或2");

books();

}

}

public void ways(int way) {

if(way==1) {

Scanner input1 = new Scanner(System.in);

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

String bookName = input1.next();

if (contains(books,bookName)==true) {

System.out.println("您要找的图书是:" + bookName);

books();

}else {

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

books();

}

}else if(way==2) {

Scanner input2 = new Scanner(System.in);

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

int bookNumber = input2.nextInt();

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

System.out.println("您要找的图书是:" + books[bookNumber-1]);

books();

}else {

System.out.println("您输入有误或图书不存在");

books();

}

}

}

private boolean contains(String[] books,String bookName) {

// TODO Auto-generated method stub

for(String find:books) {

if(find.equals(bookName)) 

return true;

}

return false;

}

}


提问者:温凉a 2018-10-07 10:46

个回答

  • qq_浩然_9
    2018-10-24 21:44:33

    不好意思啊,我也是想参考一下经验,不是应该自定义异常吗,你这里写的好像判断,

  • 邱小白
    2018-10-20 21:14:21

    写的不错!


  • 慕田峪7219739
    2018-10-13 20:26:04

    contains方法可以不写,直接遍历book,bookname.quals(book[i])也不错

  • DF10F0001A
    2018-10-13 16:36:15

    小白求问:

    if(way == 1 || way == 2){ 

    ways(way);

    这两句是啥意思

  • XxSniper
    2018-10-10 17:36:51

    能不能写个注释 亲?


  • 温凉a
    2018-10-07 10:58:50

    ?????说好的排版呢