package borrowbook;
public class BorrowBook {
public static void main(String[] args) {
Choose cs = new Choose();
cs.runChoose();
}
}
package borrowbook;
import java.util.Scanner;
public class Choose {
private String str;
private int choice;
String[] book;
public Choose() {
this.book = new String[]{"gaoshu", "sjjg", "mg", "English"};
}
public void doChoiceRight()throws Exception{
if(choice > 3 || choice < 1){
throw new Exception("命令输入错误!请根据提示输入数字命令!");
}
}
public void FindBookExist()throws Exception{
for(int i=0 ; i < book.length;i++)
{
if(str.equalsIgnoreCase(book[i])){
System.out.println("book:"+ book[i]);
return;
}
}
throw new Exception("图书不存在!");
}
public void isNumberExist()throws Exception{
if(choice > book.length || choice < 1){
throw new Exception("序号不存在!");
}else{
System.out.println("book:"+ book[choice-1]);
}
}
public void runChoose(){
while(true){
System.out.println("输入命令:1-按照书名查找图书"+
" 2-按照序号查找图书"+"3-退出");
//判断输入的命令是不是数字
try{
Scanner input = new Scanner(System.in);
choice = input.nextInt();
doChoiceRight();
}catch(Exception e){
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
}
//判断输入的数字是否有相应的选项
try{
doChoiceRight();
}catch(Exception e){
System.out.println(e.getMessage());
continue;
}
//choice保存正确的输入命令,判断choice
if(choice == 1){
System.out.println("输入图书名:");
Scanner input = new Scanner(System.in);
str = input.next();
try{
FindBookExist();
}catch(Exception e){
System.out.println(e.getMessage());
}
}else if(choice == 2){
System.out.println("输入图书序号:");
Scanner input = new Scanner(System.in);
try{
choice = input.nextInt();
}catch(Exception e){
System.out.println("命令输入错误!");
continue;
}
try{
isNumberExist();
}catch(Exception e){
System.out.println(e.getMessage());
}
}else if(choice ==3 ){
return;
}
}
}
}
点击查看更多内容
4人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦