1

如果我有多组数组,并且我想查看任何数组中是否包含两个值,我将如何去做。

这是我之前尝试过的:

public boolean multiIdTasks(int[] arrayToTest, int slayerTask, int npcKilled){
    for(int j = 0; j < arrayToTest.length; j++){
        if(arrayToTest[j] == slayerTask){
            if(confirmSlayerKill(arrayToTest, arrayToTest[j])){
                return true;
            } else {
                continue;
            }
        }
    }
    return false;
}

public boolean confirmSlayerKill(int[] arrayToTest, int npcKilled){
    for(int g = 0; g < arrayToTest.length; g++){
        if(arrayToTest[g] == npcKilled){
            return true;
        }
    }
    return false;
}

我需要检查被杀死的 npc 是否包含在给定任务的数组中。例如,如果你的任务是一只鸟,而你杀死了一只鹰,我希望减少任务的计数,但我还没有找到一个好方法来检查任务的值和被杀死的 npc 的值。

调用它的方法在这里:

public boolean getValidKills(int slayerTask, int npcKilled){
    if( multiIdTasks(SlayerData.BANSHEES, slayerTask, npcKilled) || 
        multiIdTasks(SlayerData.BATS, slayerTask, npcKilled) ||  
        multiIdTasks(SlayerData.BIRDS, slayerTask, npcKilled) || 
        multiIdTasks(SlayerData.BEARS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.CAVE_BUGS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.CAVE_SLIMES, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.COWS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.DESERT_LIZARDS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.DOGS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.DWARVES, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.GHOSTS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.GOBLINS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.ICEFIENDS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.MINOTAURS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.MONKEYS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.SCORPIONS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.SKELETONS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.SPIDERS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.TROLLS, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.WOLVES, slayerTask, npcKilled)  ||  
        multiIdTasks(SlayerData.ZOMBIES, slayerTask, npcKilled)) {
        return true;
    } else {
        return false;
    }
}

如果getValidKills为真,则计数应减少。但是我不能让它正常工作,有人可以帮忙吗?如果您需要更多信息,请告诉我...

4

1 回答 1

0
    /**
 * Search for {@code value1} and {@code value2} in {@code array} array.
 * 
 * @param array given array to be searched.
 * @param value1 value to be searched.
 * @param value2 value to be searched.
 * @return true if found both values in the given array.
 */
private static boolean hasBothValues(int[] array, int value1, int value2) {
    int totalMatches = 0;
    for (int value : array) {
        if (value == value1 || value == value2)
            totalMatches++;
        if (totalMatches >= 2)
            return true;
    }
    return false;
}
于 2012-11-04T23:30:41.513 回答