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

弄了3小时,终于搞定了。。。。。。


package bookSystem;

import java.util.Scanner;

public class bookSelectSystem {


public static void main(String[] args) {

bookSelectSystem test = new bookSelectSystem();

test.Select();

}

//####################################################

public void Select() {

String[] bookName= {"JAVA","Python","C++"};

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

Scanner input=new Scanner(System.in);

try{//检查输入匹配


int input1=input.nextInt();

if(input1!=1 && input1!=2) {//再判断

throw new Exception();//意外发现

}else if (input1 == 1){//按书名查找######

int v=0;//暂用

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

    String inputBookName = input.next();

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

    if(inputBookName.equals(bookName[i])) {//需要equals(待研究)

    System.out.println("Your book:"+bookName[i]);

    v=5;//暂用

    }

    }

    if(v==0) {

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

    throw new Exception();

    }//至此按书名查找成功实现!#############

}else if(input1==2) {//这里是按编号查找!!!!!

int bookNum;

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

try{//判断输入是否匹配int

bookNum=input.nextInt();

}catch(Exception e) {

System.out.println("请输入正确编号!!(从1开始)");

throw new Exception();

}

if(bookNum<1 || bookNum>bookName.length) {

throw new Exception();

}else {

System.out.println("Your book:"+bookName[bookNum-1]);

}

//至此按编号查找实现!!!!!!!!!!

}

    

}catch(Exception e) {

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

Select();

}

}


}



提问者:慕虎0571853 2019-01-20 16:52

个回答

  • 慕莱坞7107142
    2019-01-23 16:17:50
    已采纳

    import java.util.Scanner;
    public class Library {
     public static void main(String[] args) {
      Index();
     }
     
     public static void Index() {
      System.out.println("欢迎进入图书馆查书系统\n请输入您的指令:\n1-按名称查找图书\n2-按编号查找图书\n");
      Scanner in = new Scanner(System.in);
      int i = in.nextInt();
      
      try {
       if (i == 1) {
        System.out.println("请输入您需要查找的图书的书名");
        name();
       } else if (i == 2) {
        System.out.println("请输入您需要查找的图书的编号");
        number();
       } else {
        throw new Exception();
       }
      } catch (Exception e) {
       System.out.println("请输入正确指令\n");
       Index();
      }
     }
     
     public static void name() {
      Scanner in = new Scanner(System.in);
      String str = in.nextLine();
      int index = -1;
      try {
       for (int i = 0; i < BOOK.length; i++) {
        if (BOOK[i][1].equals(str)) {
         index = i;
         break;
        }
       }
       if (index != -1) {
        System.out.println("编号: " + BOOK[index][0] + "  书名 " + BOOK[index][1] + " 存在");
       } else {
        throw new Exception();
       }
      } catch (Exception e) {
       System.out.println("该书不存在,请重新输入正确书名......");
       name();
      }
     }
     
     public static void number() {
      Scanner in = new Scanner(System.in);
      String str = in.nextLine();
      int index = -1;
      try {
       for (int i = 0; i < BOOK.length; i++) {
        if (BOOK[i][0].equals(str)) {
         index = i;
         break;
        }
       }
       if (index != -1) {
        System.out.println("编号: " + BOOK[index][0] + "  书名 " + BOOK[index][1] + " 存在");
       } else {
        throw new Exception();
       }
      } catch (Exception e) {
       System.out.println("该书不存在,请重新输入正确编号......");
       number();
      }
     }
     
     public static String[][] BOOK = {{"0001","语文"},{"0002","数学"},{"0003","英语"},{"0004","体育"}};
    }

  • 慕函数1299684
    2019-02-09 19:38:20

    为什么这个采纳的代码中,方法和数组都写成静态的