[英]Need a return Statement? - new to coding
这个想法在拍卖中是不同的。 我知道我需要一个类型为“Lot”的退货声明,但我不确定那会是什么。 这是我的代码。
public Lot getLot(int lotNumber)
{
int index = 0;
boolean found = false;
while(index < lots.size() && !found) {
Lot selectedLot = lots.get(index);
if(selectedLot.getNumber() == lotNumber) {
found = true;
}
else {
index++;
}
}
if(!found) {
found = false;
}
}
你可以做的是类似的东西,这将做的是如果找到匹配它将返回匹配的selectedLot,如果没有找到匹配,它将返回null:
public Lot getLot(int lotNumber) {
int index = 0;
while(index < lots.size()) {
Lot selectedLot = lots.get(index);
if (selectedLot.getNumber() == lotNumber) {
return selectedLot;
} else {
index++;
}
}
return null;
}
您可以使用以下代码:
1)返回最后一批有一些数字
public Lot getLot(int lotNumber)
{
int index = 0;
Lot resultLot = null;
while(index < lots.size() ) {
Lot selectedLot = lots.get(index);
if(selectedLot.getNumber() == lotNumber) {
resultLot = selectedLot;
}
else {
index++;
}
}
return resultLot;
}
2)或(返回第一批有一些数字)
public Lot getLot(int lotNumber)
{
int index = 0;
while(index < lots.size() ) {
Lot selectedLot = lots.get(index);
if(selectedLot.getNumber() == lotNumber) {
return selectedLot;
}
else {
index++;
}
}
return null;
}
3)或(较小的例子)
public Lot getLot(int lotNumber)
{
for(Lot selectedLot: lots) {
if(selectedLot.getNumber() == lotNumber) {
return selectedLot;
}
}
return null;
}
public Lot getLot(int lotNumber) {
int index = 0;
boolean found = false;
Lot selectedLot = null; //set initial value to null
while(index < lots.size() && !found) {
selectedLot = lots.get(index);
if(selectedLot.getNumber() == lotNumber) {
found = true;
} else {
index++;
}
}
return selectedLot; //You need to return type Lot
}
当你说Lot getLot(int lotNumber)
你基本上说你要返回一个Lot类型的对象,但你永远不会在你的代码中返回它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.