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;
}
}
}
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123