【发布时间】:2014-02-26 03:09:58
【问题描述】:
public class Book {
String title;
boolean borrowed;
// Creates a new Book
public Book(String bookTitle){
bookTitle= "The Da Vinci Code";
}
// Marks the book as rented
public void borrowed() {
int borrowed= 1;
// Implement this method
}
// Marks the book as not rented
public void returned() {
int returned = 2;
// Implement this method
}
// Returns true if the book is rented, false otherwise
public boolean isBorrowed(int returned, int borrowed) {
if (borrowed < 2 )
return true;
else
return false;
// Implement this method
}
// Returns the title of the book
public String getTitle() {
String bookTitle= "The Da Vinci Code";
return bookTitle;
// Implement this method
}
public static void main(String[] arguments){
// Small test of the Book class
int returned= 1;
int borrowed= 2;
Book example = new Book("The Da Vinci Code");
System.out.println("Title (should be The Da Vinci Code): " +example.getTitle());
System.out.println("Borrowed? (should be false): " + example.isBorrowed(returned, borrowed));
example.borrowed();
System.out.println("Borrowed? (should be true): " + example.isBorrowed(returned, borrowed)); // should be returning true but it not. It printing false
example.returned();
System.out.println("Borrowed? (should be false): " + example.isBorrowed(returned, borrowed));
}
我正在做类和方法。我正在做一个书类,它几乎可以工作,但我需要让第二个打印语句打印为真,但我弄错了。我应该怎么做才能解决这个问题?
【问题讨论】:
-
你似乎不知道自己在做什么。回到绘图板。浏览最基本的教程,here。
-
您的局部变量正在阻止对类字段的访问。删除“int”声明。
标签: java class methods boolean