手记

不知道正规的写法怎么写!!!

package trycathfinally;
import java.util.Scanner;
public class BookSys {
String books_Name[]={"天书","地书","人书"};

public static void main(String[] args) {
    // TODO 自动生成的方法存根
   BookSys b1=new BookSys();
    Scanner in=new Scanner(System.in);
    System.out.println("***********************");
    System.out.println("欢迎来到vxb图书馆!");
    System.out.println("***********************");
    System.out.println("选书请输入1,退出请输入2");
    int selectNum=in.nextInt();
    try {
        b1.test(selectNum);
        if(selectNum==1){
            System.out.println("***********************");
            System.out.println("按书籍名称查找请输入1;按书籍序号查找输入2");
            int selectNum2=in.nextInt();

                b1.test(selectNum2);
                if(selectNum2==1){

                    System.out.println("***********************");
                    System.out.println("请输入书籍名称");
                    String bookName=in.next();              
                    b1.test3(bookName);
                    System.out.print("已经为你查询到:"+bookName);
                }else{
                    System.out.println("***********************");
                    System.out.println("请输入书籍序号");
                    int bookNum=in.nextInt();
                    b1.test2(bookNum);
                    System.out.print("已经为你查询到:"+b1.books_Name[bookNum-1]);

                }

        }else if(selectNum==2){

        System.out.println("谢谢使用!88");
        System.out.println("***********************");  

        }
    } catch (Exception e1) {
        // TODO 自动生成的 catch 块
        e1.printStackTrace();
    }

}

public void test(int i) throws Exception{

   if(!(i>=1&&i<=2)){
       throw new Exception("你的输入有错误");

   }

}
public void test2(int i) throws Exception{
i=i-1;
if(!(i>=0&&i<books_Name.length)){
throw new Exception("你的序号输入有错误");

   }

}

public void test3(String a) throws Exception{
boolean pd=false;

     for(int j=0;j<3;j++){
           if(a.equals(books_Name[j])){
            pd=true;     
           } 
     }
  if(!pd){

      throw new Exception("你输入的书名有错误");

  } 

}

}

3人推荐
随时随地看视频
慕课网APP