弄了3小時(shí),終于搞定了。。。。。。
package bookSystem;
import java.util.Scanner;
public class bookSelectSystem {
public static void main(String[] args) {
bookSelectSystem test = new bookSelectSystem();
test.Select();
}
//####################################################
public void Select() {
String[] bookName= {"JAVA","Python","C++"};
System.out.println("輸入命令:1-按書(shū)名查找 2-按編號(hào)查找");? ??
Scanner input=new Scanner(System.in);
try{//檢查輸入匹配
int input1=input.nextInt();
if(input1!=1 && input1!=2) {//再判斷
throw new Exception();//意外發(fā)現(xiàn)
}else if (input1 == 1){//按書(shū)名查找######
int v=0;//暫用
System.out.println("請(qǐng)輸入書(shū)名:");
? ? String inputBookName = input.next();
? ? for(int i=0;i<bookName.length;i++) {
? ? if(inputBookName.equals(bookName[i])) {//需要equals(待研究)
? ? System.out.println("Your book:"+bookName[i]);
? ? v=5;//暫用
? ? }
? ? }
? ? if(v==0) {
? ? System.out.println("此書(shū)不存在!!");
? ? throw new Exception();
? ? }//至此按書(shū)名查找成功實(shí)現(xiàn)!#############
}else if(input1==2) {//這里是按編號(hào)查找?。。。?!
int bookNum;
System.out.println("請(qǐng)輸入編號(hào):");
try{//判斷輸入是否匹配int
bookNum=input.nextInt();
}catch(Exception e) {
System.out.println("請(qǐng)輸入正確編號(hào)??!(從1開(kāi)始)");
throw new Exception();
}
if(bookNum<1 || bookNum>bookName.length) {
throw new Exception();
}else {
System.out.println("Your book:"+bookName[bookNum-1]);
}
//至此按編號(hào)查找實(shí)現(xiàn)?。。。。。。。。?!
}
? ??
}catch(Exception e) {
System.out.println("——情根據(jù)提示輸入正確命令!——");
Select();
}
}
}
2019-01-23
import java.util.Scanner;
public class Library {
?public static void main(String[] args) {
??Index();
?}
?
?public static void Index() {
??System.out.println("歡迎進(jìn)入圖書(shū)館查書(shū)系統(tǒng)\n請(qǐng)輸入您的指令:\n1-按名稱查找圖書(shū)\n2-按編號(hào)查找圖書(shū)\n");
??Scanner in = new Scanner(System.in);
??int i = in.nextInt();
??
??try {
???if (i == 1) {
????System.out.println("請(qǐng)輸入您需要查找的圖書(shū)的書(shū)名");
????name();
???} else if (i == 2) {
????System.out.println("請(qǐng)輸入您需要查找的圖書(shū)的編號(hào)");
????number();
???} else {
????throw new Exception();
???}
??} catch (Exception e) {
???System.out.println("請(qǐng)輸入正確指令\n");
???Index();
??}
?}
?
?public static void name() {
??Scanner in = new Scanner(System.in);
??String str = in.nextLine();
??int index = -1;
??try {
???for (int i = 0; i < BOOK.length; i++) {
????if (BOOK[i][1].equals(str)) {
?????index = i;
?????break;
????}
???}
???if (index != -1) {
????System.out.println("編號(hào): " + BOOK[index][0] + "? 書(shū)名 " + BOOK[index][1] + " 存在");
???} else {
????throw new Exception();
???}
??} catch (Exception e) {
???System.out.println("該書(shū)不存在,請(qǐng)重新輸入正確書(shū)名......");
???name();
??}
?}
?
?public static void number() {
??Scanner in = new Scanner(System.in);
??String str = in.nextLine();
??int index = -1;
??try {
???for (int i = 0; i < BOOK.length; i++) {
????if (BOOK[i][0].equals(str)) {
?????index = i;
?????break;
????}
???}
???if (index != -1) {
????System.out.println("編號(hào): " + BOOK[index][0] + "? 書(shū)名 " + BOOK[index][1] + " 存在");
???} else {
????throw new Exception();
???}
??} catch (Exception e) {
???System.out.println("該書(shū)不存在,請(qǐng)重新輸入正確編號(hào)......");
???number();
??}
?}
?
?public static String[][] BOOK = {{"0001","語(yǔ)文"},{"0002","數(shù)學(xué)"},{"0003","英語(yǔ)"},{"0004","體育"}};
}
2019-02-09
為什么這個(gè)采納的代碼中,方法和數(shù)組都寫(xiě)成靜態(tài)的