交作業(yè)~~歡迎暴力測(cè)試,提出改進(jìn)。
package AA;
import java.util.Scanner;
public class Main {
? ? static String[] s = {"紅樓夢(mèng)","水滸傳","西游記","三國(guó)演義"};
public static void main(String[] args) {
select();
}
? ? public static void select(){
? ? System.out.println("請(qǐng)輸入命令:1-按照書(shū)籍名稱(chēng)查找書(shū)籍。2-按照書(shū)籍序號(hào)查找圖書(shū)");
? ? 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("請(qǐng)輸入圖書(shū)名稱(chēng):");
? ? try{
? ? Scanner sc = new Scanner(System.in);
? ? ? ? String x = sc.next();
? ? ? ? name(x);
? ? ? ? if(x.equals(s[0])){
? ? ? ? System.out.println("書(shū)籍:"+s[0]);
? ? ? ? System.out.println("是否繼續(xù)查找書(shū)籍?1-是。2-否");
? ? ? ? int a = sc.nextInt();
? ? ? ? if(a == 1){ select(); }
? ? ? ? else{ System.out.println("歡迎下次光臨!"); }
? ? ? ? }else if(x.equals(s[1])){
? ? ? ? System.out.println("書(shū)籍:"+s[1]);
? ? ? ? System.out.println("是否繼續(xù)查找書(shū)籍?1-是。2-否");
? ? ? ? int a = sc.nextInt();
? ? ? ? if(a == 1){ select(); }
? ? ? ? else{ System.out.println("歡迎下次光臨!"); }
? ? ? ? }else if(x.equals(s[2])){
? ? ? ? System.out.println("書(shū)籍:"+s[2]);
? ? ? ? System.out.println("是否繼續(xù)查找書(shū)籍?1-是。2-否");
? ? ? ? int a = sc.nextInt();
? ? ? ? if(a == 1){ select(); }
? ? ? ? else{ System.out.println("歡迎下次光臨!"); }
? ? ? ? }else if(x.equals(s[3])){
? ? ? ? System.out.println("書(shū)籍:"+s[3]);
? ? ? ? System.out.println("是否繼續(xù)查找書(shū)籍?1-是。2-否");
? ? ? ? int a = sc.nextInt();
? ? ? ? if(a == 1){ select(); }
? ? ? ? else{ System.out.println("歡迎下次光臨!"); }
? ? ? ? }else{
? ? ? ? System.out.println("此書(shū)不存在!");
? ? ? ? Name();
? ? ? ? }
? ? }catch(Exception e){
? ? System.out.println(e.getMessage());
? ? select();
? ? }
? ? }
? ? public static void Num(){
? ? System.out.println("請(qǐng)輸入圖書(shū)序號(hào):");
? ? 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("是否繼續(xù)查找書(shū)籍?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("是否繼續(xù)查找書(shū)籍?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("是否繼續(xù)查找書(shū)籍?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("是否繼續(xù)查找書(shū)籍?1-是。2-否");
? ? ? ? ? int a = sc.nextInt();
? ? ? ? ? if(a == 1){
? ? ? ? ? select();
? ? ? ? ? }
? ? ? ? ? else{
? ? ? ? ? System.out.println("歡迎下次光臨!");
? ? ? ? ? }
? ? ? ? }
? ? else{
? ? System.out.println("此序號(hào)不存在!");
? ? 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("圖書(shū)不存在");
? ? }
? ? return x;
? ? }
? ? public static int num(int y)throws Exception{
? ? if(y<0||y>3){
? ? throw new Exception("圖書(shū)不存在");
? ? }
? ? return y;
? ? }
}
2020-08-10
/***
1、循環(huán)體+return? 可以在用戶(hù)輸入錯(cuò)誤時(shí)可以在選擇輸入,退出時(shí)可以退出整個(gè)程序。
2、catch 出的錯(cuò)誤,我們提示“錯(cuò)誤”,不要用系統(tǒng)的錯(cuò)誤,用戶(hù)體驗(yàn)更好。
*/
?public static void select(){
? ? System.out.println("請(qǐng)輸入命令:1-按照書(shū)籍名稱(chēng)查找書(shū)籍。2-按照書(shū)籍序號(hào)查找圖書(shū)");
while(true){
try{
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
if(a == 1){ Name();
????return;? //退出
}else if(a == 2){ Num();
return; //退出
}else{
System.out.println("你輸入的命令有誤!");
//select();
}
}catch(Exception e){
//e.printStackTrace();
System.out.println("請(qǐng)輸入1、2選擇操縱");
}
}
}