[英]Cannot figure out why this curly brace is giving out an error
public String checkIn(Book b) {
for (int i = 0; i < books.length; i++) {
if (b.equals(books[i])) {
if (checkedOut[i] > 0) {
checkedOut[i]--;
return "Checked in!";
}
else {
return "All of our copies are already checked in.";
}
}
else {
return "Book not found.";
}
}
}
}
The last curly brace is where I get the error. 最后一个花括号是我得到错误的地方。 What's odd is that it recommends i remove it yet it still gives out the error.
奇怪的是,它建议我将其删除,但仍然会显示错误。
Any ideas? 有任何想法吗?
Few Mistakes. 几乎没有错误。
1.) One curly brace is extra
in the last. 1.)最后
One curly brace is extra
的。
2.) Also add return statement
at the end of function. 2.)还要在函数末尾添加
return statement
。
You have one too many braces. 您的括号过多。 Remove the last one.
删除最后一个。 I would recommend formatting your code, as it will make it easier to read and fix problems like these in the future.
我建议格式化您的代码,因为这将使将来更容易阅读和解决此类问题。
public String checkIn(Book b){
for(int i = 0; i < books.length; i++){
if(b.equals(books[i])){
if(checkedOut[i] > 0){
checkedOut[i]--;
return "Checked in!";
}else{
return "All of our copies are already checked in.";
}
}else{
return "Book not found.";
}
}
return "";
}
If you are not using an IDE such as Netbeans or Eclipse , I would recommend using one as they will help with your errors. 如果您没有使用诸如Netbeans或Eclipse之类的IDE,我建议您使用一个,因为它们会帮助您解决错误。
Check with the below code 检查以下代码
public String checkIn(Book b)
{
for (int i = 0; i < books.length; i++)
{
if (b.equals(books[i]))
{
if (checkedOut[i] > 0)
{
checkedOut[i]--;
return "Checked in!";
}
else
{
return "All of our copies are already checked in.";
}
}
else
{
return "Book not found.";
}
}
}
Your Code 您的密码
public String checkIn(Book b)
{
for (int i = 0; i < books.length; i++)
{
if (b.equals(books[i]))
{
if (checkedOut[i] > 0)
{
checkedOut[i]--;
return "Checked in!";
}
else
{
return "All of our copies are already checked in.";
}
}
else
{
return "Book not found.";
}
}
return "something";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.