弄了3小時,終于搞定了。。。。。。
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-按書名查找 2-按編號查找");? ??
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){//按書名查找######
int v=0;//暫用
System.out.println("請輸入書名:");
? ? 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("此書不存在!!");
? ? throw new Exception();
? ? }//至此按書名查找成功實現(xiàn)!#############
}else if(input1==2) {//這里是按編號查找?。。。?!
int bookNum;
System.out.println("請輸入編號:");
try{//判斷輸入是否匹配int
bookNum=input.nextInt();
}catch(Exception e) {
System.out.println("請輸入正確編號?。?從1開始)");
throw new Exception();
}
if(bookNum<1 || bookNum>bookName.length) {
throw new Exception();
}else {
System.out.println("Your book:"+bookName[bookNum-1]);
}
//至此按編號查找實現(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("歡迎進入圖書館查書系統(tǒng)\n請輸入您的指令:\n1-按名稱查找圖書\n2-按編號查找圖書\n");
??Scanner in = new Scanner(System.in);
??int i = in.nextInt();
??
??try {
???if (i == 1) {
????System.out.println("請輸入您需要查找的圖書的書名");
????name();
???} else if (i == 2) {
????System.out.println("請輸入您需要查找的圖書的編號");
????number();
???} else {
????throw new Exception();
???}
??} catch (Exception e) {
???System.out.println("請輸入正確指令\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("編號: " + BOOK[index][0] + "? 書名 " + BOOK[index][1] + " 存在");
???} else {
????throw new Exception();
???}
??} catch (Exception e) {
???System.out.println("該書不存在,請重新輸入正確書名......");
???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("編號: " + BOOK[index][0] + "? 書名 " + BOOK[index][1] + " 存在");
???} else {
????throw new Exception();
???}
??} catch (Exception e) {
???System.out.println("該書不存在,請重新輸入正確編號......");
???number();
??}
?}
?
?public static String[][] BOOK = {{"0001","語文"},{"0002","數(shù)學(xué)"},{"0003","英語"},{"0004","體育"}};
}
2019-02-09
為什么這個采納的代碼中,方法和數(shù)組都寫成靜態(tài)的