import java.awt.TexturePaint;
import java.util.InputMismatchException;
import java.util.Scanner;
public class error extends Exception {
String maths="高数";
String linux="操作系统";
public void test(){
for(;;) {
try{
System.out.println("1.按名称查找。2.按序号查找");
Scanner imput=new Scanner(System.in);
int num=imput.nextInt();
if(num==1){
System.out.println("请输入名称:");
Scanner book=new Scanner(System.in);
String boo=book.next();
if(boo.equals("高数")){
System.out.println("书籍为高数");
}
}
if(num==2){
}
}catch (InputMismatchException e) {
System.out.println("请输入1或者2");
continue;
}
break;
}
}
public static void main(String[] args) {
error i=new error();
i.test();
}
}
num调用了Scanner对象imput的nextInt()方法,只有在你输入不为int类型时才会抛出InputMismatchException异常