首页 > 解决方案 > 无法获取特定捐赠者的 searchBook 方法

问题描述

该程序的目标是输入书名、捐赠者、章节数和位置。从那里,用户为以下提示输入他们的输入:“对于搜索,请输入捐赠者的姓名:________”。我能够将所有这些信息作为输出。但是,我似乎无法弄清楚如何将结果缩小到一个捐赠者。相反,我的程序会打印出条目中列出的所有捐赠者。到目前为止,我已经在下面列出了我的程序以及我的示例输出和我想要的输出。

谢谢!

  package Books;
    
    import java.util.Scanner;
    
    public class Books {
              
            String title;
            String donatedBy;
            int numChapters;
            String bookLocation;
    
            void Book(){
    
                title="ava Programming";
    
                donatedBy="James Bond";
    
                numChapters=25;
                
                bookLocation = "A-007";
    
            }
    
            Books(String title, String donatedBy, int numChapters, String bookLocation){
    
                 this.title=title;
                 this.donatedBy=donatedBy;
                 this.bookLocation=bookLocation;
                   if(numChapters<1){
                     this.numChapters=25;
                    }
                    else{
                        this.numChapters=numChapters;
                    }
                }
                public String getTitle() {
                    return title;
                }
                public String getDonatedBy() {
                    return donatedBy;
                }
                public int getNumChapters() {
                    return numChapters;
                }
                public void setDonatedBy(String donatedBy) {
                    this.donatedBy = donatedBy;
                }
                public void setNumChapters(int numChapters) {
                    this.numChapters = numChapters;
                }
                public void setTitle(String title) {
                    this.title = title;
}
                public void printDetails(){
                    System.out.print("Title: \t"+title+" \nDonor: \t"+donatedBy+" \nChapters: "+numChapters + "\nLocation: " + bookLocation);
                }
                public static void main(String[] args) {
                    Scanner scanner = new Scanner(System.in);
                    System.out.print("Enter number of Books: ");
                    int num = scanner.nextInt();
                    scanner.nextLine();
                    Books[] books = new Books[num];
    
                    for (int i = 0; i < num; i++) {
                        System.out.print("Enter the title: ");
                        String title = scanner.nextLine();
                        System.out.print("Enter the donor: ");
                        String donor = scanner.nextLine();
                        System.out.print("Enter the number of chapters: ");
                        int chaps = scanner.nextInt();

                        
                        scanner.nextLine();
                        books[i] =new Books(title, donor, chaps, bookLocation);
                    }
                    
                    boolean flag=false;
    
                    for(int i=0; i<books.length;i++){
                        Books b=books[i];
                        System.out.print("\n\nBook "+(i+1)+": \n");
                        b.printDetails();
                        
                        if (b.getDonatedBy().equals("Daisy")){
                            flag=true;
                        
        
                }
                scanner.close();
            }

标签: java

解决方案


heloo,我希望我不要说一些愚蠢的话,但是如果我理解这一行“b.printDetails();” 它打印出你想要的细节,它不应该在那个 if 子句下吗?“b.getDonatedBy().match..” 因为只有这样,您才想打印详细信息。


推荐阅读