qq_MEGALOVANIA_0
2018-01-19 12:03:47浏览 2883
package com.imooc;
import java.util.Scanner;
//图书借记you异常
public class Demo06 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Book[] books={new Book(1,"Chinese"),new Book(2,"Math"),
new Book(3,"English"),new Book(4,"java"),new Book(5,"C语言")};
System.out.println("*****************图书借记系统*******************");
Scanner input=new Scanner(System.in);
int a=1;
Demo06 dd=new Demo06();
do{
System.out.println("输入命令(1-按照书名查找 2-按照ID查找 3-退出):");
int command=input.nextInt();
switch(command){
case 1:
System.out.println("输入你要查找的图书的名字:");
String name=input.next();
dd.nameFind(name, books);
break;
case 2:
System.out.println("输入你要查找到图书的ID:");
int id=input.nextInt();
dd.idFind(id, books);
break;
case 3:
System.out.println("再见!");
a=0;
break;
default:
System.out.println("输入了错误的命令!");
}
}while(a==1);
input.close();
}
public void nameFind(String name,Book[] books){
try{
int i;
for(i=0;i<books.length;i++){
if(name.equals(books[i].getName())){
break;
}
}
System.out.println("你要查找的book:"+books[i].getName());
}catch(Exception e){
System.out.println("异常:根据书名,未找到图书信息!");
}
}
public void idFind(int id,Book[] books){
int i;
try{
for(i=0;i<books.length;i++){
if(id==books[i].getId()){
break;
}
}
System.out.println("你要查找的book:"+books[i].getName());
}catch(Exception e){
System.out.println("异常:根据图书ID,未找到图书信息!");
}
}
}
热门评论
Book[] books = {new...}这是什么用法?生成一个字典?后面的getName()和getId()两个方法也报错,是要引入什么包吗?不是太明白