package book;
public class Book {
String bookName;//书名
int bookNum;//图书序号
public void work() {
System.out.println("book:"+bookName);
}
public Book(String bookName,int bookNum) {
this.bookName = bookName;
this.bookNum = bookNum;
}
}
package book;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Book[] books= {
new Book("论语",1),
new Book("高数",2),
new Book("数据结构",3),
new Book("三字经",4),
new Book("新华字典",5),
};
//选择类型
Scanner input = new Scanner(System.in);
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
try {
int option1 = input.nextInt();
if(option1==1) {//名称查找书
int j =0;//定义j储存遍历结果
System.out.println("请输入图书名称:");
String option2 = input.next();
for(int i = 1 ; i<=books.length ; i++) {
if(option2==books[i-1].bookName) {
j = 1;
}
}
System.out.println("J的值为:"+j);//局部变量j.....
if(j==1) {
for(int i = 1 ; i<=books.length ; i++) {
if(option2==books[i-1].bookName) {
books[i-1].work();
}
}
}else{
System.out.println("图书不存在!");
}
}else if(option1==2) {//按照序号查找书
System.out.println("请输入图书序号:");
int option3 = input.nextInt();
if(option3>=1&&option3<=books.length) {
for(int i = 1 ; i<=books.length ; i++) {
if(option3==i){
books[i-1].work();
}
}
}else {
System.out.println("图书不存在!");
}
}
}catch(Exception e){
e.printStackTrace();
System.out.println("命令输入错误!请根据提示输入数字命令!");
}
}
}
楼上老陈
相关分类