作业提交,大家给提提意见,有什么简单点的方法实现吗

来源:1-9 经验总结

java菜鸟工程师

2019-05-07 19:14

package com.imooc.exception;

import java.util.Arrays;
import java.util.Scanner;

public class BorrowBook {
    public static void main(String[] args) {
        BorrowBook borrowBook=new BorrowBook();
       borrowBook.Borrow();
    }

    public void Borrow(){
        // 书号分别对应的是1,2,3,4
        String[] books={"西游记","红楼梦","三国演义","水浒传"};
        System.out.println("欢迎使用图书管理系统!");
        System.out.println("1. 按照书名查找图书       2.按照序号查找图书");
        Scanner input =new Scanner(System.in);
        try {
           int num=input.nextInt();
           if (num==1){
               System.out.println("请输入书名:");
               Scanner input1=new Scanner(System.in);
               String bookname=input1.nextLine();
               boolean book= Arrays.asList(books).contains(bookname);
               if (book==true){
                   System.out.println(bookname+"借书成功!");
               }else {
                   throw new NoBookException("此书不存在");
               }
           }
           if (num==2){
               System.out.println("请输入书号:");
               Scanner input2=new Scanner(System.in);
               int shuhao=input2.nextInt();

                   if (shuhao>0 && shuhao<=books.length ){
                       System.out.println(books[shuhao-1]+"借书成功!");
                   }else{
                       throw new NoBookException("查无此书!");
                   }
           }
        }catch (NoBookException e){
            System.out.println(e);
            System.out.println();
            Borrow();
        }
    }
}


写回答 关注

4回答

  • 大神godbig
    2019-07-28 15:42:37

    你的shuhao应该>=0,就比如36行,应该这么写:if (shuhao>=0  && shuhao<books.length )。因为数组下标从0开始

  • qq_慕沐4213791
    2019-07-23 11:29:07

    这个序号是自动就给定好了的吗?

    拆鸡

    他拿的是索引吗????

    2019-07-31 11:22:14

    共 1 条回复 >

  • 杨小羊冲丫
    2019-07-21 11:05:54

    package com.imooc.exception; import java.util.Arrays;import java.util.Scanner; public class BorrowBook {    public static void main(String[] args) {        BorrowBook borrowBook=new BorrowBook();       borrowBook.Borrow();    }     public void Borrow(){        // 书号分别对应的是1,2,3,4        String[] books={"西游记","红楼梦","三国演义","水浒传"};        System.out.println("欢迎使用图书管理系统!");        System.out.println("1. 按照书名查找图书       2.按照序号查找图书");        Scanner input =new Scanner(System.in);        try {           int num=input.nextInt();           if (num==1){               System.out.println("请输入书名:");               Scanner input1=new Scanner(System.in);               String bookname=input1.nextLine();               boolean book= Arrays.asList(books).contains(bookname);               if (book==true){                   System.out.println(bookname+"借书成功!");               }else {                   throw new NoBookException("此书不存在");               }           }           if (num==2){               System.out.println("请输入书号:");               Scanner input2=new Scanner(System.in);               int shuhao=input2.nextInt();                    if (shuhao>0 && shuhao<=books.length ){                       System.out.println(books[shuhao-1]+"借书成功!");                   }else{                       throw new NoBookException("查无此书!");                   }           }        }catch (NoBookException e){            System.out.println(e);            System.out.println();            Borrow();        }    }}

  • tiger爱小狄
    2019-05-08 09:45:13

    你这个已经挺简单了,要是借书前加个循环输出都有哪些书号+书名,就更好了

    拆鸡

    大哥 你那边这想法有完成吗 我和你同样的想法

    2019-07-31 11:24:09

    共 1 条回复 >

Java入门第三季

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

409792 学习 · 4340 问题

查看课程

相似问题