gpt4 book ai didi

java - 缺少返回语句方法ReturnCopy

转载 作者:行者123 更新时间:2023-12-01 10:18:43 33 4
gpt4 key购买 nike

我创建了这个方法,但我不确定为什么它说缺少返回语句。我需要将打印更改为返回吗? (这是最底部的方法)我是一个 Java 初学者,所以任何帮助将不胜感激!

public class Book {
private String title;
private String author;
private int copies;
private boolean borrowed;


public Book( String inAuthor, String inTitle, int inNumberOfCopies ) {
this.author = inAuthor;
this.title = inAuthor;
this.copies = inNumberOfCopies;

}

public void borrowed() {
borrowed = true;
}

public void rented() {
borrowed = true;
}

public void returned() {
borrowed = false;
}

public boolean isBorrowed() {
return borrowed;
}

public String getAuthor() {
return this.author;

}

public static String getTitle() {
return getTitle();

}

public int getTotalCopies() {
return this.copies;

}

public int getAvailableCopies() {

}

public void withdrawCopy() {
int found = 0;
for (Book b : Library.getListOfBooks()) {
if (b.getTitle().equals(title)) {
if (found == 0) {
found = 1;
}
if (!b.isBorrowed()) {
b.borrowed=true;
found = 2;
break;
}
if (found == 0) {
System.out.println("Sorry, this book is not in our catalog.");
} else if (found == 1) {
System.out.println("Sorry, this book is already borrowed.");
} else if (found == 2) {
System.out.println("You successfully borrowed " + title);
}
}

}

}

public String returnCopy() {
boolean found = false;
for (Book book : Library.getListOfBooks()) {
if (getTitle().equals(title) && book.isBorrowed()) {
book.returned();
found = true;
}
if (found) {
System.out.println("you successfully returned " + title);
}
}
}
}

最佳答案

public String returnCopy()
public 后面的

String 表示该方法将返回一个 String。您的 public String returnCopy() 当前未返回任何内容。

如果你不想返回任何东西,你可以像这样使用void:

public void returnCopy(){
// code
}

public int getAvailableCopies() 存在同样的问题,这应该返回一个 int 但您没有返回任何内容。

小心:

这个方法:

public static String getTitle() {
return getTitle();
}

是一种没有基本条件的递归方法。这将导致错误并迫使您的应用程序崩溃。

关于java - 缺少返回语句方法ReturnCopy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756859/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com