作业问题:为什么输入a的时候会无限循环,catch不能接收错误吗?麻烦大神们讲解一下~万分感谢
public class BookMagSystem {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc= new Scanner(System.in);
while(true){
String [] book={"数据结构","数据库","高数"};
System.out.println("欢迎来到图书管理系统");
System.out.println("1-按照书名查找图书;2-按照序号查找图书");
try{
int se=sc.nextInt();
if(se==1)
test1(book);
if(se==2)
test2(book);
}catch(Exception e){
System.out.println("命令输入错误!!");
}
}
}
public static void test1(String[] book){
try{
System.out.println("输入图书名称:");
Scanner sc1= new Scanner(System.in);
String name=sc1.nextLine();
for(int i=0;i<book.length;i++){
if(name.equals(book[i]))
System.out.println("book:"+book[i]);
}
}catch(Exception e){
System.out.println("图书不存在!");
}
}
public static void test2(String[] book) throws Exception{
System.out.println("输入图书序号:");
Scanner sc2= new Scanner(System.in);
int num=sc2.nextInt();
for(int i=0;i<book.length;i++){
if(num==i+1)
System.out.println("book:"+book[i]);
else
throw new Exception("图书不存在!!");
}
}
}