[英]Returning specific array object within a method definition
I need to determine which predator in my PredatorList
array has received the most damage. 我需要确定PredatorList
数组中哪个掠食者受到的伤害最大。 For some reason when I try to return that predator, eclipse says - mostDamaged
cannot be resolved to a variable. 由于某种原因,当我尝试返回该捕食者时,eclipse表示mostDamaged
无法解析为变量。
Why is this so? 为什么会这样呢?
public Predator mostDamagedPredator() {
// test if PredatorList contains predators
if (PredatorList.length > 0){
float difference = 0;
for (int i = 0; i < PredatorList.length; i++) {
if (PredatorList[i].getMaxHitPoints() - PredatorList[i].getHitPoints() > difference){
Predator mostDamaged = PredatorList[i];
}
}
return mostDamaged; // ERROR - mostDamaged cannot be resolved to a variable
}
// return null if there are no predators in PredatorList
return null;
}
You declared mostDamaged
inside an if
statement block, so it's not within scope outside that block. 您在if
语句块内声明了mostDamaged
,因此它不在该块外的范围内。
Move it outside : 将其移到外面:
public Predator mostDamagedPredator() {
if (PredatorList.length > 0){
float difference = 0;
Predator mostDamaged = null;
for (int i = 0; i < PredatorList.length; i++) {
if (PredatorList[i].getMaxHitPoints() - PredatorList[i].getHitPoints() > difference){
mostDamaged = PredatorList[i];
}
}
return mostDamaged;
}
return null;
}
or a little better : 或更好一点:
public Predator mostDamagedPredator() {
Predator mostDamaged = null;
if (PredatorList.length > 0){
float difference = 0;
for (int i = 0; i < PredatorList.length; i++) {
if (PredatorList[i].getMaxHitPoints() - PredatorList[i].getHitPoints() > difference){
mostDamaged = PredatorList[i];
}
}
}
return mostDamaged;
}
You have declared mostDamaged variable inside for context. 您已在上下文中声明了mostDamaged变量。
Declare it out, and initialize it there: 声明并在那里初始化:
public Predator mostDamagedPredator() {
// test if PredatorList contains predators
if (PredatorList.length > 0){
float difference = 0;
Predator mostDamaged = null;
for (int i = 0; i < PredatorList.length; i++) {
if (PredatorList[i].getMaxHitPoints() - PredatorList[i].getHitPoints() > difference){
mostDamaged = PredatorList[i];
}
}
return mostDamaged; // ERROR - mostDamaged cannot be resolved to a variable
}
// return null if there are no predators in PredatorList
return null;
} }
This is because mostDamaged
is defined within the if
statement of your for
loop. 这是因为mostDamaged
是在for
循环的if
语句中定义的。 This means where you want to return it the variable is not defined. 这意味着您要在其中返回的变量未定义。
You can rewrite the method like this: 您可以这样重写方法:
public Predator mostDamagedPredator() {
// test if PredatorList contains predators
Predator mostDamaged = null; // initialize it with null
if (PredatorList.length > 0){
float difference = 0;
for (int i = 0; i < PredatorList.length; i++) {
if (PredatorList[i].getMaxHitPoints() - PredatorList[i].getHitPoints() > difference){
mostDamaged = PredatorList[i]; // assign the correct item from the array
}
}
}
// either mostDamaged was initialized in the if statement or it is still null
return mostDamaged;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.