源自:1-9 经验总结
哪位大佬看看,为哈输入字母不会执行异常,输入其他数字会执行异常
package immmmm;
import java.util.Arrays;
import java.util.Scanner;
public class YC {
public static void main(String[] args) {
YC a=new YC();
a.zhu();
}
public void zhu() {
String sz[] = {"高数", "数据结构", "大学物理", "java", "网络安全", "冲啊少年",};
YC rent = new YC();
System.out.println("输入命令:1—按照名称查找图书;2—按照序号查找图书");
Scanner got = new Scanner(System.in);
int x=got.nextInt();
try {
if (x == 1) {
System.out.println("输入图书名称:");
String y = got.next();
boolean status = Arrays.toString(sz).contains(y);
if (status) {
System.out.println("book:" + y);
} else {
throw new SMexception();
}
} else if (x == 2) {
System.out.println("输入图书序号:");
int z = got.nextInt();
if (z >= 0 || z <= sz.length) {
System.out.println("book:" + sz[z]);
} else {
throw new zfexception();
}
} else {
throw new Myexception();
}
} catch (Myexception e) {
System.out.println("命令输入错误!请根据提示输入数字!");
rent.zhu();
} catch (SMexception e) {
System.out.println("图书不存在!");
rent.zhu();
} catch (zfexception e) {
System.out.println("图书不存在!");
}
}
}
提问者:慕盖茨0163116
2019-03-16 17:49