嗨,我必须编程:
public class Book {
String title;
boolean borrowed;
// Creates a new Book
public Book(String bookTitle) {
// Implement this method
}
// Marks the book as rented
public void borrowed() {
// Implement this method
}
// Marks the book as not rented
public void returned() {
// Implement this method
}
// Returns true if the book is rented, false otherwise
public boolean isBorrowed() {
// Implement this method
}
// Returns the title of the book
public String getTitle() {
// Implement this method
}
public static void main(String[] arguments) {
// Small test of the Book class
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());
example.rented();
System.out.println("Borrowed? (should be true): " + example.isBorrowed());
example.returned();
System.out.println("Borrowed? (should be false): " + example.isBorrowed());
}
}
输出应该是这样的:
Title (should be The Da Vinci Code): The Da Vinci Code
Rented? (should be false): false
Rented? (should be true): true
Rented? (should be false): false
我确实喜欢这个:
public class Book {
String title;
boolean borrowed;
// Creates a new Book
public Book(String bookTitle) {
// Implement this method
String book=new String();
}
// Marks the book as rented
public void borrowed() {
// Implement this method
borrowed=true;
}
// Marks the book as not rented
public void returned() {
// Implement this method
borrowed=false;
}
// Returns true if the book is rented, false otherwise
public boolean isBorrowed() {
// Implement this method
if (book=borrowed()){
return true;
}else if(book==returned()){
return false;
}
}
// Returns the title of the book
public String getTitle() {
// Implement this method
return "The Da Vinci Code";
}
public static void main(String[] arguments) {
// Small test of the Book class
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());
System.out.println("Borrowed? (should be true): " + example.isBorrowed());
System.out.println("Borrowed? (should be false): " + example.isBorrowed());
}
}
我认为我的代码通常很好,但是eclipse说:书籍无法解析为变量..在以下部分:
public boolean isBorrowed() {
// Implement this method
if (book=borrowed()){
return true;
}else if(book==returned()){
return false;
}
}
如何更正前一部分代码中的问题? 我是否对其余的代码做了更正? 感谢
答案 0 :(得分:4)
让我们放大这段代码:
if (book=borrowed()) {
return true;
} else if(book==returned()) {
return false;
}
首先要注意的是,在使用==
检查相等性时,=
运算符用于分配。例如,borrowed == true
检查borrowed
是否为真,borrowed = true
将borrowed
分配为真。
要注意的第二件事是,该书甚至不是isBorrowed
中可用的变量,因为您没有将其保存为实例变量,就像您对borrowed
和title
所做的那样。
要注意的第三件事是您的支票book == borrowed()
和book == returned()
没有意义。 borrowed()
和returned()
是void
方法,因此它们不返回任何内容,而book是一个字符串。您真正想要检查的是变量borrowed
是真还是假:
if (borrowed == true) {
return true;
} else if (borrowed == false) {
return false;
}
可以简化为:
return borrowed;
您还需要更改main
方法以在打印语句之间调用适当的方法:
System.out.println("Title (should be The Da Vinci Code): " + example.getTitle());
System.out.println("Borrowed? (should be false): " + example.isBorrowed());
example.borrowed();
System.out.println("Borrowed? (should be true): " + example.isBorrowed());
example.returned();
System.out.println("Borrowed? (should be false): " + example.isBorrowed());
答案 1 :(得分:0)
您没有在book
中定义isBorrowed()
变量,因此它未定义,您应该收到编译器错误。
即使您有book
变量,isBorrowed()
方法也太复杂了。只需返回borrowed
。
您还需要在分别拨打第二个borrowed()
之前和之后拨打returned()
和isBorrowed()
,以进行正确的测试。
此外,在构造函数中设置title
变量。
this.title = bookTitle;
并在title
中返回getTitle()
,而不是对其进行硬编码。