我是这样写的,大家给评价一下
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();
}
}
}
}