Java 在if语句中使用布尔方法时遇到问题
我想使用Book类中的isLong()方法,并在BookCollection方法中的displayLongBooks()中使用它。类BookCollection具有bookList变量。变量bookList是ArrayList类型,包含所有书籍的集合。ArrayList中的所有对象都是书本类型Java 在if语句中使用布尔方法时遇到问题,java,Java,我想使用Book类中的isLong()方法,并在BookCollection方法中的displayLongBooks()中使用它。类BookCollection具有bookList变量。变量bookList是ArrayList类型,包含所有书籍的集合。ArrayList中的所有对象都是书本类型 import java.util.ArrayList; public class BookCollection { ArrayList<Book> bookList =
import java.util.ArrayList;
public class BookCollection
{
ArrayList<Book> bookList = new ArrayList<Book>();
public BookCollection(){
bookList.add(new Book(1001-01-141514, "Letones", "CS", 611));
bookList.add(new Book(1002-01-141424, "Lewis", "CS", 477));
bookList.add(new Book(1003-01-141434, "Smith", "MATH", 698));
bookList.add(new Book(1004-01-141444, "Smith", "CS", 617));
bookList.add(new Book(1005-01-141454, "Brown", "CHEM", 326));
bookList.add(new Book(1006-01-141464, "Smith", "BIO", 127));
bookList.add(new Book(1007-01-141474, "Sanket", "CS", 998));
}
public String toString()
{
}
public void displayLongBooks()
{
System.out.println();
System.out.println("LONG BOOKS");
if (isLong() == true)
System.out.println(bookList);
}
public void displayBooksFromAuthor(String author)
{
}
public void displayBooksFromArea(String area)
{
}
public void displayAverageLength()
{
}
}
import java.util.*;
import java.io.*;
public class Book
{
String author;
String area;
int isbn;
int pages;
public Book (int isbn, String author, String area, int pages)
{
this.isbn = isbn;
this.author = author;
this.area = area;
this.pages = pages;
}
public boolean isLong()
{
if(pages>600)
return true;
else
return false;
}
public String toString()
{
return "ISBN: " + this.isbn + "Author: " + this.author
+ "Subject: " + this.area + "Pages: " + this.pages;
}
/**PrintWriter outfile = new PrintWriter (new FileWriter("books.txt"));
outfile.write("100101141514, Letones, CS, 611");
outfile.write(100201141424, Lewis, CS, 477);
outfile.write(100301141434, Smith, MATH, 698);
outfile.write(100401141444, Smith, CS, 617);
outfile.write(100501141454, Brown, CHEM, 326);
outfile.write(100601141464, Smith, BIO, 127);
outfile.write(100701141474, Sanket, CS, 998);
outfile.close();**/
}
import java.util.ArrayList;
公共类藏书
{
ArrayList bookList=新建ArrayList();
公共藏书(){
图书目录。添加(新书(1001-01-141514,“Letones”,“CS”,611);
图书目录。添加(新书(1002-01-141424,“刘易斯”,“CS”,477));
图书目录。添加(新书(1003-01-141434,“史密斯”,“数学”,698));
图书目录。添加(新书(1004-01-141444,“史密斯”,“CS”,617));
图书目录。添加(新书(1005-01-141454,“棕色”,“化学”,326));
图书目录。添加(新书(1006-01-141464,“史密斯”,“生物”,127));
图书目录。添加(新书(1007-01-141474,“Sanket”,“CS”,998));
}
公共字符串toString()
{
}
公共图书馆
{
System.out.println();
System.out.println(“长卷”);
如果(isLong()==true)
系统输出打印项次(书目);
}
public void displayBooksFromAuthor(字符串作者)
{
}
公共空白显示区域(字符串区域)
{
}
public void displayAverageLength()
{
}
}
导入java.util.*;
导入java.io.*;
公共课堂用书
{
字符串作者;
串区;
国际标准书号;
整版;
公共图书(整数isbn、字符串作者、字符串区域、整数页)
{
这是isbn=isbn;
this.author=作者;
这个面积=面积;
this.pages=页面;
}
公共布尔isLong()
{
如果(页面>600)
返回true;
其他的
返回false;
}
公共字符串toString()
{
返回“ISBN:+this.ISBN+”Author:+this.Author
+主题:“+this.area+”页面:“+this.Pages;
}
/**PrintWriter outfile=新的PrintWriter(新的FileWriter(“books.txt”);
输出文件。写入(“100101141514,Letones,CS,611”);
输出文件写入(100201141424,刘易斯,CS,477);
outfile.write(1003011141434,史密斯,数学,698);
输出文件写入(1004011141444,史密斯,CS,617);
outfile.write(100501141454,布朗,化学,326);
outfile.write(100601141464,史密斯,生物,127);
输出文件写入(100701141474,桑克特,CS,998);
outfile.close()**/
}
听起来您需要为书籍
(s)迭代图书列表
,并调用Book.isLong()
。使用一个看起来像
public void displayLongBooks()
{
System.out.println();
System.out.println("LONG BOOKS");
for (Book b : bookList) { // <-- for each Book b in bookList
if (b.isLong()) { // no need for == true
System.out.println(b); // <-- b, not bookList
}
}
}
public void display longbooks()
{
System.out.println();
System.out.println(“长卷”);
对于(Book b:bookList){/if(isLong()==true)
-No.Justif(isLong())
。当我们进行此操作时,System.out.println(bookList);
的缩进是错误的。同样,isLong()
应该简单地返回页面>600;
这已经是布尔值(true或false)。ifisLong()
是在Book
上定义的方法,您需要在Book
的实例上调用它,例如Book.isLong()
。另外,1001-01-141514
是-140514
。可能不是你想要的。这个问题似乎是离题的,因为它是关于面向对象编程的基本知识。解释对象和方法如何工作太广泛了,可以在很多(官方)网站上找到在线教程。嘿,谢谢你,Elliott。我还有一个问题。你知道如何将书籍输入到文件中吗?在代码的底部,我有一个变量outfile,它应该将每本书写入file books.txt,但我得到了预期的错误。@Matt它不在方法中。我不完全知道你的意思。@Matt我的意思是它是o之外的原始代码你希望它什么时候运行,为什么?