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));
}
我正在上课和方法。我正在制作一本书课,它几乎可以工作但是我需要让第二个印刷语句打印出来,但我得到的是假的。我该怎么做才能解决这个问题?
答案 0 :(得分:0)
你在void中使用了一个新的int,并且它与main方法中的int不同,所以把它作为int类的一个属性,不要声明它两次。
答案 1 :(得分:-1)
您在borrowed
方法中将局部变量main()
设置为2。您调用isBorrowed()
并将此变量传递给它。然后在isBorrowed()
中,如果borrowed
小于2,则表示返回true。不是这样,你返回false。
您的borrowed()
方法需要将borrowed
中的Book
字段设置为true,而不是设置局部变量。
您应该了解声明变量和将值分配给变量
之间的区别