代码的布尔部分在 java 中不起作用
boolean part of the code does not work in java
嗨,我必须编写这个程序:
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 说:book cannot be resolved to a variable.. in the following part :
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
是否为真,borrowed = true
将 borrowed
赋值给真。
第二点要注意的是 book 甚至不是 isBorrowed
中可用的变量,因为您没有像 borrowed
和 [=22= 那样将其保存为实例变量].
第三点要注意的是你的检查 book == borrowed()
和 book == returned()
没有意义。 borrowed()
和 returned()
是 void
方法,所以它们不会 return 任何东西,而 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());
您没有在 isBorrowed()
中定义 book
变量,所以它是未定义的,您应该收到一个编译器错误。
即使您有一个 book
变量,isBorrowed()
方法也太复杂了。就 return borrowed
.
您还需要在调用第二个 isBorrowed()
之前和之后分别调用 borrowed()
和 returned()
以进行适当的测试。
此外,在构造函数中设置 title
变量。
this.title = bookTitle;
和 return title
在 getTitle()
中,而不是对其进行硬编码。
嗨,我必须编写这个程序:
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 说:book cannot be resolved to a variable.. in the following part :
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
是否为真,borrowed = true
将 borrowed
赋值给真。
第二点要注意的是 book 甚至不是 isBorrowed
中可用的变量,因为您没有像 borrowed
和 [=22= 那样将其保存为实例变量].
第三点要注意的是你的检查 book == borrowed()
和 book == returned()
没有意义。 borrowed()
和 returned()
是 void
方法,所以它们不会 return 任何东西,而 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());
您没有在 isBorrowed()
中定义 book
变量,所以它是未定义的,您应该收到一个编译器错误。
即使您有一个 book
变量,isBorrowed()
方法也太复杂了。就 return borrowed
.
您还需要在调用第二个 isBorrowed()
之前和之后分别调用 borrowed()
和 returned()
以进行适当的测试。
此外,在构造函数中设置 title
变量。
this.title = bookTitle;
和 return title
在 getTitle()
中,而不是对其进行硬编码。