交作业~~欢迎暴力测试,提出改进。
package AA;
import java.util.Scanner;
public class Main {
static String[] s = {"红楼梦","水浒传","西游记","三国演义"};
public static void main(String[] args) {
select();
}
public static void select(){
System.out.println("请输入命令:1-按照书籍名称查找书籍。2-按照书籍序号查找图书");
try{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if(a == 1){ Name();
}else if(a == 2){ Num();
}else{
System.out.println("你输入的命令有误!");
select();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void Name(){
System.out.println("请输入图书名称:");
try{
Scanner sc = new Scanner(System.in);
String x = sc.next();
name(x);
if(x.equals(s[0])){
System.out.println("书籍:"+s[0]);
System.out.println("是否继续查找书籍?1-是。2-否");
int a = sc.nextInt();
if(a == 1){ select(); }
else{ System.out.println("欢迎下次光临!"); }
}else if(x.equals(s[1])){
System.out.println("书籍:"+s[1]);
System.out.println("是否继续查找书籍?1-是。2-否");
int a = sc.nextInt();
if(a == 1){ select(); }
else{ System.out.println("欢迎下次光临!"); }
}else if(x.equals(s[2])){
System.out.println("书籍:"+s[2]);
System.out.println("是否继续查找书籍?1-是。2-否");
int a = sc.nextInt();
if(a == 1){ select(); }
else{ System.out.println("欢迎下次光临!"); }
}else if(x.equals(s[3])){
System.out.println("书籍:"+s[3]);
System.out.println("是否继续查找书籍?1-是。2-否");
int a = sc.nextInt();
if(a == 1){ select(); }
else{ System.out.println("欢迎下次光临!"); }
}else{
System.out.println("此书不存在!");
Name();
}
}catch(Exception e){
System.out.println(e.getMessage());
select();
}
}
public static void Num(){
System.out.println("请输入图书序号:");
try{
Scanner sc = new Scanner(System.in);
int y = sc.nextInt();
num(y);
if(y==0){
System.out.println("book:"+s[0]);
System.out.println("是否继续查找书籍?1-是。2-否");
int a = sc.nextInt();
if(a == 1){ select(); }
else{ System.out.println("欢迎下次光临!"); }
}
else if(y==1){
System.out.println("book:"+s[1]);
System.out.println("是否继续查找书籍?1-是。2-否");
int a = sc.nextInt();
if(a == 1){ select(); }
else{ System.out.println("欢迎下次光临!"); }
}
else if(y==2){
System.out.println("book:"+s[2]);
System.out.println("是否继续查找书籍?1-是。2-否");
int a = sc.nextInt();
if(a == 1){
select();
}
else{
System.out.println("欢迎下次光临!");
}
}
else if(y==3){
System.out.println("book:"+s[3]);
System.out.println("是否继续查找书籍?1-是。2-否");
int a = sc.nextInt();
if(a == 1){
select();
}
else{
System.out.println("欢迎下次光临!");
}
}
else{
System.out.println("此序号不存在!");
Num();
}
}catch(Exception e){
System.out.println(e.getMessage());
select();
}
}
public static String name(String x)throws Exception{
if(!(x.equals(s[0]))&&!(x.equals(s[1]))&&!(x.equals(s[2]))&&!(x.equals(s[3]))){
throw new Exception("图书不存在");
}
return x;
}
public static int num(int y)throws Exception{
if(y<0||y>3){
throw new Exception("图书不存在");
}
return y;
}
}