package?rentbook;
import?java.util.Scanner;
public?class?Rent?{
public?static?void?main(String[]?args)?{
System.out.println("***歡迎來到租書系統(tǒng),請(qǐng)根據(jù)有效提示進(jìn)行選擇!***");
System.out.println("1-按照名稱查找圖書;2-按照序號(hào)查找圖書");
Scanner?input?=?new?Scanner(System.in);
Book[]?book?=?{?new?Book(1,?"數(shù)據(jù)結(jié)構(gòu)"),?new?Book(2,?"高數(shù)"),
new?Book(3,?"算法"),?new?Book(4,?"論語")?};
int?x?=?input.nextInt();
if?(x?==?1)?{
System.out.println("輸入圖書名稱:");
String?y?=?input.next();
for?(int?i?=?0;?i?<?book.length;?i++)?{
Book?book1?=?(Book)?book[i];
try?{
if?(y.equals(book1.getBookName()))?{
System.out.println(y);
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
}?else?if?(x?==?2)?{
System.out.println("輸入圖書序號(hào):");
int?z?=?input.nextInt();
for?(int?i?=?0;?i?<?book.length;?i++)?{
Book?book1?=?(Book)?book[i];
if?(z?==?(book1.getBookNum()))?{
System.out.println(book[book1.getBookNum()-1].getBookName());
}
else{
System.out.println("圖書不存在");
}
}
}?else?{
System.out.println("command?is?wrong!");
}
}
}package rentbook;public class Book { private String bookName; private int bookNum; public Book(int bookNum, String bookName) { this.bookNum=bookNum; this.bookName=bookName; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getBookNum() { return bookNum; } public void setBookNum(int bookNum) { this.bookNum = bookNum; } }
我每次循環(huán)時(shí)都會(huì)把結(jié)果打印出來,求大神解決一下問題,代碼可運(yùn)行
你已走遠(yuǎn)
2016-09-15 12:30:04