返回方法定义中的特定数组对象

时间:2016-09-25 10:45:59

标签: java arrays

我需要确定PredatorList数组中哪个捕食者受到的伤害最大。出于某种原因,当我试图返回那个捕食者时,eclipse说 - mostDamaged无法解析为变量。

为什么会这样?

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;
}

3 个答案:

答案 0 :(得分:1)

您在mostDamaged语句块中声明了if,因此它不在该块之外的范围内。

将其移到外面:

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;
}

或更好一点:

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;
}

答案 1 :(得分:1)

您已在内部声明了mostDamaged变量。

宣布它,然后在那里初始化:

// (Requires Promise support in your browser)
"use strict";
var createDirPromise = createPromiseFunction("createDirPromise");
var download1 = createPromiseFunction("download1");
var download2 = createPromiseFunction("download2");
var download3 = createPromiseFunction("download3");

function downloadAll(request, response) {
  return createDirPromise().then(function(result) {
      return Promise.all([download1(/*contentsJson*/), download2(/*contentsJson*/), download3(/*contentsJson*/)]);
    })
    .then(function(result) {
      // **** Here, the result of download1 is result[0]
      var statusJson = result[0];
      console.log("statusJson = '" + statusJson + "'");
    });
}
downloadAll();

function createPromiseFunction(name) {
  return function() {
    return new Promise(function(resolve) {
      setTimeout(function() {
        console.log("resolving " + name);
        resolve("result of " + name);
      }, Math.random() * 50);
    });
  };
}

}

答案 2 :(得分:0)

这是因为mostDamaged是在if循环的for语句中定义的。这意味着您要返回它的位置,未定义变量。

您可以像这样重写方法:

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;
}