我每次循环时都会把结果打印出来,求大神解决一下问题,代码可运行
package rentbook;import java.util.Scanner;public class Rent { public static void main(String[] args) { System.out.println("***欢迎来到租书系统,请根据有效提示进行选择!***"); System.out.println("1-按照名称查找图书;2-按照序号查找图书"); Scanner input = new Scanner(System.in); Book[] book = { new Book(1, "数据结构"), new Book(2, "高数"), new Book(3, "算法"), new Book(4, "论语") }; int x = input.nextInt(); if (x == 1) { System.out.println("输入图书名称:"); String y = input.next(); for (int i = 0; i < book.length; i++) { Book book1 = (Book) book; try { if (y.equals(book1.getBookName())) { System.out.println(y); } } catch (Exception e) { e.printStackTrace(); } } } else if (x == 2) { System.out.println("输入图书序号:"); int z = input.nextInt(); for (int i = 0; i < book.length; i++) { Book book1 = (Book) book; if (z == (book1.getBookNum())) { System.out.println(book[book1.getBookNum()-1].getBookName()); } else{ System.out.println("图书不存在"); } } } else { System.out.println("command is wrong!"); } }}package rentbook;
public class Book {
private String bookName;
private int bookNum;
public Book(int bookNum, String bookName) {
this.bookNum=bookNum;
this.bookName=bookName;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getBookNum() {
return bookNum;
}
public void setBookNum(int bookNum) {
this.bookNum = bookNum;
}
} |
免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。
版权声明:作者保留权利,不代表天盟立场。
|
|
|
|
你这个需求好像都有点混乱,两种查询方式,应该是不管哪种查询,最后查到了都要输出书籍的编号和名字,没查到就提示输出没找到把。 |
|
|
|
|