谁有很好的代码,方法用的都是try-catch,而不是都用if语句,我的代码不能按照名称查找,有什么问题
谁有很好的代码供参考呀,我的代码输入序号还可以,但是按照名称查找就是输不出来,输出都是图书不存在
public class jieshu {
String[] book={"语文","数学","英语","物理","化学","生物"};
Scanner input=new Scanner(System.in);
public void test1(){
System.out.println("输入图书名称");
String name=input.next();
try {
for(int i=0;i<book.length;i++){
if(name.equals(book[i])){
System.out.println("book:"+book[i]);
}else{
} throw new Exception();
}
}catch (Exception e) {
System.out.println("图书不存在异常");
}
}
public void test2(){
System.out.println("输入图书序号");
int number=input.nextInt();
try {
if(number<=book.length){
System.out.println("book:"+book[number-1]);
}else{
throw new Exception();
}
} catch (Exception e) {
System.out.println("图书不存在");
}
}
public void test(){
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
try {
int score=input.nextInt();
if(score==1){
this.test1();
}
if(score==2){
this.test2();
}else{
throw new Exception();
}
} catch (Exception e) {
System.out.println("命令输入错误,请根据提示输入!");
this.test();
}
}
public static void main(String[] args) {
jieshu a=new jieshu();
a.test();
}
}