我是這樣寫的,大家給評價一下
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, "高數"), new Shu(2, "線性代數"),
new Shu(3, "數據庫"), new Shu(4, "統籌學"), };
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("命令輸入錯誤,請根據提示輸入正確的數字命令");
break;
}
finalbook.getName().getClass();
System.out.println("book:" + finalbook.getName());
} catch (NumberFormatException e) {
System.out.println("命令輸入錯誤,請根據提示輸入數字命令");
// e.printStackTrace();
continue;
} catch (NullPointerException e) {
System.out.println("圖書不存在!");
// TODO: handle exception
continue;
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}
}
2015-12-15
我是初學者,很多東西都正在學,這個程序我看了下應該是可以實現的,也沒發現什么錯誤!