我是這樣寫的,大家給評價一下
package com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.sun.istack.internal.FinalArrayList;
public class Jieyue {
/**
* @param args
*/
public static void main(String[] args) {
Shu[] books = { new Shu(1, "高數(shù)"), new Shu(2, "線性代數(shù)"),
new Shu(3, "數(shù)據(jù)庫"), new Shu(4, "統(tǒng)籌學(xué)"), };
BufferedReader input = new BufferedReader(new InputStreamReader(
System.in));
boolean i = true;
int commend = 0;
Shu finalbook = new Shu();
while (i) {
System.out.println("輸入命令:1-按照名稱進行查詢圖書;2-按照序號進行查詢圖書" + "\n");
try {
commend = Integer.parseInt(input.readLine());
switch (commend) {
case 1:
System.out.println("輸入圖書名稱:" + "\n");
String bookname = input.readLine();
for (Shu book : books) {
if (book.getName().equals(bookname)) {
finalbook = book;
}
}
break;
case 2:
System.out.println("輸入圖書序號:" + "\n");
int booknum = Integer.parseInt(input.readLine());
for (Shu book : books) {
if (book.getNum() == booknum) {
finalbook = book;
}
}
break;
default:
System.out.println("命令輸入錯誤,請根據(jù)提示輸入正確的數(shù)字命令");
break;
}
finalbook.getName().getClass();
System.out.println("book:" + finalbook.getName());
} catch (NumberFormatException e) {
System.out.println("命令輸入錯誤,請根據(jù)提示輸入數(shù)字命令");
// e.printStackTrace();
continue;
} catch (NullPointerException e) {
System.out.println("圖書不存在!");
// TODO: handle exception
continue;
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}
}
2015-12-15
我是初學(xué)者,很多東西都正在學(xué),這個程序我看了下應(yīng)該是可以實現(xiàn)的,也沒發(fā)現(xiàn)什么錯誤!