弄了3小时,终于搞定了。。。。。。
package bookSystem;
import java.util.Scanner;
public class bookSelectSystem {
public static void main(String[] args) {
bookSelectSystem test = new bookSelectSystem();
test.Select();
}
//####################################################
public void Select() {
String[] bookName= {"JAVA","Python","C++"};
System.out.println("输入命令:1-按书名查找 2-按编号查找");
Scanner input=new Scanner(System.in);
try{//检查输入匹配
int input1=input.nextInt();
if(input1!=1 && input1!=2) {//再判断
throw new Exception();//意外发现
}else if (input1 == 1){//按书名查找######
int v=0;//暂用
System.out.println("请输入书名:");
String inputBookName = input.next();
for(int i=0;i<bookName.length;i++) {
if(inputBookName.equals(bookName[i])) {//需要equals(待研究)
System.out.println("Your book:"+bookName[i]);
v=5;//暂用
}
}
if(v==0) {
System.out.println("此书不存在!!");
throw new Exception();
}//至此按书名查找成功实现!#############
}else if(input1==2) {//这里是按编号查找!!!!!
int bookNum;
System.out.println("请输入编号:");
try{//判断输入是否匹配int
bookNum=input.nextInt();
}catch(Exception e) {
System.out.println("请输入正确编号!!(从1开始)");
throw new Exception();
}
if(bookNum<1 || bookNum>bookName.length) {
throw new Exception();
}else {
System.out.println("Your book:"+bookName[bookNum-1]);
}
//至此按编号查找实现!!!!!!!!!!
}
}catch(Exception e) {
System.out.println("——情根据提示输入正确命令!——");
Select();
}
}
}