來交作業(yè)了
import java.util.Scanner;
public class Initial {
String? [] BookList=new String[] {"高數(shù)","Java","數(shù)據(jù)結(jié)構(gòu)","C++","計算機(jī)網(wǎng)絡(luò)"};
public static void main(String[] args) {
Initial init=new Initial();
init.start();
}
public void start() {
? ?int a=1;
ChooseMethod();
}
public void ChooseMethod() {
System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");
Scanner methodinput=new Scanner(System.in);
try {
int method=methodinput.nextInt();
if(method==1)
SearchByName();
if (method==2)
SearchByNumber();
} catch (NameException e) {
System.out.println(e.getMessage());
start();
}catch (NumberException e) {
System.out.println(e.getMessage());
start();
}catch (Exception e) {
System.out.println("命令輸入錯誤!請按照提示輸入數(shù)字命令!");
start();
}
}
public void SearchByNumber() throws NumberException{
System.out.println("輸入圖書序號:");
Scanner BookNumberInput=new Scanner(System.in);
int BookNumber = 0;
try {
BookNumber = BookNumberInput.nextInt();
} catch (Exception e) {
System.out.println("命令輸入錯誤!請按照提示輸入數(shù)字命令!");
SearchByNumber();
}
if(BookNumber<=BookList.length)
System.out.println("book:"+BookList[BookNumber-1]);
else
throw new NumberException();
}
public void SearchByName() throws NameException{
System.out.println("輸入圖書名稱:");
Scanner BooknameInput=new Scanner(System.in);
String Bookname=BooknameInput.nextLine();
Boolean check=false;
for(int i=0;i<BookList.length;i++) {
if(BookList[i].equals(Bookname))
{
System.out.println("book:"+Bookname);
check=true;
}
}
if(check==false)
throw new NameException();
}
}
public class NameException extends Exception{
NameException(){
super("圖書不存在!");
}
}
public class NumberException extends Exception{
public NumberException() {
super("圖書不存在!");
}
}
2020-03-08
厲害,向你學(xué)習(xí)