大神看一下这个怎么改,开头输入0和1以外的居然无限循环,输入书名都异常,,,
package com.Zyc;
import java.util.InputMismatchException;
import java.util.Scanner;
public class book {
public static void main(String[] args) throws InputMismatchException {
String[] books={"数据结构","操作系统","JAVA","算法","LINUX","WEB前端"};
Scanner input=new Scanner(System.in);
String z = null;
while(z==null){
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
try {
int a=input.nextInt();
if (a==1) {
System.out.println("输入图书名称:");
String b=input.next();
for(int i=0;i<books.length;i++){
if (b.equals(books[i])) {
z="book:"+books[i];
System.out.println(z);
}
else{
throw new Exception();
}
}
} else if (a==2) {
System.out.println("输入图书序号:");
int c=input.nextInt();
for(int i=0;i<books.length;i++){
if (c==i+1) {
z="books:"+books[i];
System.out.println(z);
}else if (c>=books.length) {
throw new ArrayIndexOutOfBoundsException();
}
}
}
else {
throw new InputMismatchException();
}
} catch (InputMismatchException e) {
System.out.println("命令错误输入错误!请根据提示输入数字命令!");
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("图书不存在");
}catch (Exception e) {
System.out.println("图书不存在");
}
}
}
}