Java 返回正确或错误的方法 公共类书籍{ 字符串标题; 布尔借用; //创建一本新书 公共书籍(字串书名){ bookTitle=“达芬奇密码”; } //将该书标记为已出租 公共无效借用(){ int=1; //实现这个方法 } //将该书标记为未出租 返回的公共无效(){ 返回的int=2; //实现这个方法 } //如果书籍是出租的,则返回true,否则返回false 公共布尔值是借来的(int返回,int借来){ 如果(借用
我在做类和方法。我在做一个book类,它几乎可以工作了,但是我需要得到第二个print语句来打印true,但是我得到false。我应该怎么做来解决这个问题呢?在Java 返回正确或错误的方法 公共类书籍{ 字符串标题; 布尔借用; //创建一本新书 公共书籍(字串书名){ bookTitle=“达芬奇密码”; } //将该书标记为已出租 公共无效借用(){ int=1; //实现这个方法 } //将该书标记为未出租 返回的公共无效(){ 返回的int=2; //实现这个方法 } //如果书籍是出租的,则返回true,否则返回false 公共布尔值是借来的(int返回,int借来){ 如果(借用,java,class,methods,boolean,Java,Class,Methods,Boolean,我在做类和方法。我在做一个book类,它几乎可以工作了,但是我需要得到第二个print语句来打印true,但是我得到false。我应该怎么做来解决这个问题呢?在main()方法中将局部变量lowed设置为2。调用islowed()并将此变量传递给它。然后在isfollowed()中,如果followed小于2,则返回true。事实并非如此,因此返回false 您的followed()方法需要将Book中的followed字段设置为true,而不是设置局部变量 您应该了解声明变量和为变量赋值之间的
main()
方法中将局部变量lowed
设置为2。调用islowed()
并将此变量传递给它。然后在isfollowed()
中,如果followed
小于2,则返回true。事实并非如此,因此返回false
您的followed()
方法需要将Book
中的followed
字段设置为true,而不是设置局部变量
您应该了解声明变量和为变量赋值之间的区别您在void followed中创建了一个新的int,它与main方法中的int不同,因此将其作为类int followed的属性,不要声明两次。您似乎不知道自己在做什么。返回到d绘图板。浏览最基本的教程。您的局部变量阻止了对类字段的访问。删除“int”声明。
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));
}