嗨,我必须对此进行编程:
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());
}
}
我认为我的代码总体上不错,但是日食说:book不能解析为变量..在以下部分中:
public boolean isBorrowed() {
// Implement this method
if (book=borrowed()){
return true;
}else if(book==returned()){
return false;
}
}
如何解决代码前面部分中的问题?
我是否对其余代码进行了纠正?
谢谢
最佳答案
让我们放大这段代码:
if (book=borrowed()) {
return true;
} else if(book==returned()) {
return false;
}
首先要注意的是在检查是否使用
==
时是否相等,=
运算符用于赋值。例如,borrowed == true
检查borrowed
是否为true,并且borrowed = true
将borrowed
分配为true。要注意的第二件事是,书甚至不是
isBorrowed
中可用的变量,因为您没有像对borrowed
和title
那样将其保存为实例变量。要注意的第三件事是您的支票
book == borrowed()
和book == returned()
没有意义。 borrowed()
和returned()
是void
方法,因此它们不返回任何内容,而book是字符串。您真正要检查的是变量borrowed
是true还是false: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());
关于java - 代码的 boolean 部分在Java中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32362014/