package com.project3;
public class Book {
String [] book ={"语文","数学","英语","生物","物理","化学"};
}
package com.project3;
import java.util.Scanner;
public class bookSystem {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎来到借书系统O~O");
System.out.println("图书列表:");
System.out.println("序号"+"\t"+"书名");
Book bk = new Book();
int i = 0;
for(String BK : bk.book){
System.out.println(i+"\t"+BK);
i++;
}
for(;;){
try{
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
int input = Input();
switch(input){
case -1: System.out.println("命令输入错误!请输入int数据类型!");
continue;
case 1: String name = getbyname(bk.book);
System.out.println("book:"+name);
in.close();
break;
case 2: String name2 = getbynum(bk.book);
System.out.println("book:"+name2);
in.close();
break;
default: System.out.println("命令输入错误!请按照提示输入命令!");
continue;
}
break;
}catch(Exception e){
System.out.println(e.getMessage());
continue;
}
}
}
public static String getbyname(String[] book) throws Exception{
for(;;){
try{
System.out.print("请输入书名:");
String input1 = in.next();
for(String b:book){
if(input1.equals(b)){
return b;
}
}
throw new Exception();
}catch(Exception e){
System.out.println("图书不存在!");
}
}
}
public static String getbynum(String[] book)throws Exception{
for(;;){
try{
System.out.print("请输入序号:");
int input2 = Input();
if(input2 == -1){
System.out.println("命令输入错误!请输入int数据类型!");
continue;
}else if(0<=input2&&input2<book.length){
return book[input2];
}
throw new Exception();
}catch(Exception e){
System.out.println("图书不存在!");
}
}
}
public static int Input(){
try {
int input = in.nextInt();
return input;
} catch (Exception e) {
// TODO: handle exception
in = new Scanner(System.in);
return -1;
}
}
}
点击查看更多内容
13人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦