交作业,欢迎改进
import java.util.Scanner;
public class jieShu {
static String shu[] = {"西游记","三国演义","水浒传","红楼梦"};
public static void main(String[] args) {
System.out.println("欢迎进入借书系统!");
jie();
}
public static void jie () {
System.out.println("请选择找书方式:1.按序号查找,2.按名称查找");
try {
Scanner input = new Scanner(System.in);
int x = input.nextInt();
if (x == 1) {
num();
}else if (x == 2) {
name();
}else {
System.out.println("您输入错误!");
jie();
}
}catch(Exception e) {
System.out.println("请按操作执行!");
jie();
}
}
public static void num() {
try {
System.out.print("请输入序号:");
Scanner input = new Scanner(System.in);
int x = input.nextInt();
if (x==1 || x==2 || x==3 || x==4) {
System.out.println("您借的书为:"+shu[x-1]);
System.out.println("是否继续借书?1.是,2.否。");
int y = input.nextInt();
if (y == 1) {
jie();
}else {
System.out.println("欢迎下次光临!");
}
}else {
System.out.println("请输入1-4之间的整数");
}
}catch(Exception e) {
System.out.println("请输入正确的数字!");
num();
}
}
public static void name() {
try {
System.out.print("请输入名称:");
Scanner input = new Scanner(System.in);
String x = input.next();
if (x.equals(shu[0]) || x.equals(shu[1]) || x.equals(shu[2]) || x.equals(shu[3])) {
System.out.println("您借的书为:"+x);
System.out.println("是否继续借书?1.是,2.否。");
int y = input.nextInt();
if (y == 1) {
jie();
}else {
System.out.println("欢迎下次光临!");
}
}else {
System.out.println("您输入的书名不存在,请重新输入。");
name();
}
}catch(Exception e) {
System.out.println("请输入正确的书名!");
name();
}
}
}