import java.util.;
/
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
public class BookTry {
Scanner input = new Scanner(System.in);
private String[] bookName = {"高数","数据结构","C语言"};
public void search1() throws BookException{
System.out.println("输入图书名称:");
String k = input.nextLine();
Boolean flag = false;
for(int i =0; i<3; i++){
if(k.equals(bookName[i])){
System.out.println("book:"+bookName[i]);
flag = true;
}
}
if(!flag)
throw new BookException("图书不存在,请重新输入!!!");
}
public void search2() throws BookException{
System.out.println("输入图书序号:");
try{
Scanner inp = new Scanner(System.in);
int k = inp.nextInt();
if(k>=0&&k<3)
System.out.println("book:"+bookName[k]);
else
throw new BookException("图书不存在,请重新输入!!!");
}catch(InputMismatchException ime){
System.out.println("命令输入错误! 请重新输入!");
search2();
}
}
public void start(){
Scanner in = new Scanner(System.in);
System.out.print("输入命令:1-按名称查找图书;2-按序号查找图书");
try{
int j = in.nextInt();
if(j == 1)
search1();
else if(j == 2)
search2();
}catch(InputMismatchException ime){
System.out.println("命令输入错误! 请重新输入!");
start();
}catch(BookException be){
System.out.println(be.getMessage());
start();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BookTry bookTry = new BookTry();
try {
bookTry.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class BookException extends Exception {
public BookException(){
super();
}
public BookException(String message){
super(message);
}
}
热门评论
真垃圾啊。都是错的代码