java - 无法获取特定捐赠者的 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();
}
解决方案
heloo,我希望我不要说一些愚蠢的话,但是如果我理解这一行“b.printDetails();” 它打印出你想要的细节,它不应该在那个 if 子句下吗?“b.getDonatedBy().match..” 因为只有这样,您才想打印详细信息。
推荐阅读
- java - Java Spring如何模拟实现相同接口的bean
- r - 如何替换一个单词,除非它紧跟另一个单词
- entity-framework - 实体框架保存相关数据外键问题
- javascript - Puppeteer iFrame 处理
- python - 如何使用 python 在 matplotlib 中绘制子图,如图所示
- mysql - Loopback :我们如何使用与 MySql Db 连接的单个和多个文件创建 fileUpload 并使用 forigen 键将数据存储在 Mysql 中
- python - 使用“devtool modify”修改openbmc代码
- sql - 获取调用我的过程的嵌套过程的名称
- clojure - Clojure 传递依赖版本冲突
- python - while 循环的函数输出结果给出了错误的值