新手寫代碼,可能比較繁復(fù)
public class book {
public static void main(String[] args) throws book_dontexist, error_command
{
book book1=new book();
book1.borrow_book1();
}
public? void borrow_book1() throws book_dontexist, error_command
{
//book book1 = new book();
System.out.println("1-按照名稱查找圖書;2-按照序號查找圖書");
Scanner input=new Scanner(System.in);
int? command=input.nextInt();
if(command!=1&&command!=2)
{
try {
? ? ? ? throw new error_command("命令輸入錯誤!請根據(jù)提示輸入命令");
? ? ? ? ?}
? ? catch(error_command m) {
? ? ?
? ? }
? ? finally {
? ? borrow_book1();
? ? }
}
String[] library ={"小王子","玻璃城堡","悲慘世界","心理罪","殺死一只知更鳥"};
if(command==1)
{
? ?System.out.println("輸入圖書名稱");
? ?int m=0;
? ?Scanner input1=new Scanner(System.in);
? ?String bookname=input1.next();
? ? ? ?for(int i=0;i<library.length;i++)
? ? ? ?{
? ? ? ?
? ? ? ?if(bookname.contentEquals(library[i]))
? ? {
? ?m++;
? ?System.out.println("book:"+bookname);// System.out.println(library[i]);
? ? }?
? ? ? ? ?if((i==library.length-1)&&(m==0))
? ? ? ? ?{
? ? ? ? try {
? ? ? ? throw new book_dontexist("圖書不存在");
? ? ? ? ?}
? ? catch(book_dontexist e) {
? ? ?
? ? }
? ? finally {
? ? borrow_book1();
? ? }
? ? ? ? ?}
? ? ? ? ?
? ? ? ?}
}
if(command==2)
{
? ?System.out.println("輸入圖書序號");
? ?int m=0;
? ?Scanner input2=new Scanner(System.in);
? ?int? num=input2.nextInt();
? ? ? ?
? ? ? ?
? ? ? ?if(num<library.length)
? ? {
? ?System.out.println("book:"+library[num]);// System.out.println(library[i]);
? ? }?
? ? ? ?else?
? ? ? ? ?{
? ? try {
? ? ? ? throw new book_dontexist("圖書不存在");
? ? ? ? ?}
? ? catch(book_dontexist e) {
? ?
? ? }
? ? finally {
? ? borrow_book1();
? ? }
? ? ? ? ?}
}
? }
}
2019-09-06
類最好是首字母大寫