感觉不是很好。。。继续努力
package com.imook;
import java.util.Scanner;
public class Book {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int j;
bookName []info={new bookName("数据结构",1),new bookName("高数", 2)};
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
Scanner si=new Scanner(System.in);
String ss= si.next();
Integer it=null;
try{
it= Integer.parseInt(ss);
// System.out.println(it);
if(it.equals(1)){
System.out.println("输入图书名称:");
String name=si.next();
for(j=0;j<2;j++){
if(name.equals(info[j].getName())){
info[j].show();
break;
}
}
if(j==2)
System.out.println("图书不存在");
}
else if(it.equals(2)){
Integer num;
while(true){
System.out.println("输入图书序号:");
String sss= si.next();
num=null;
try{
num= Integer.parseInt(sss);
for(j=0;j<2;j++){
if(num==info[j].getI()){
info[j].show();
break;
}
}
if(j==2)
System.out.println("图书不存在");
}catch(Exception e){
System.out.println("命令输入错误,请根据提示输入数字命令!");
}
}
}
}catch(NumberFormatException e){
System.out.println("命令输入错误,请根据提示输入数字命令!");
}
}
}
package com.imook;
public class bookName {
private String name;
private int i;
public bookName(String name,int i){
this.setName(name);
this.setI(i);
}
public void show(){
System.out.println("book:"+name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}