如何在int数组中找到两个不同的值?

时间:2012-11-04 21:42:31

标签: java arrays

如果我有多组数组,并且我想查看任何数组中是否包含两个值,我将如何进行此操作。

这是我以前尝试过的:

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 killed是否包含在给定任务的数组中。例如,如果你的任务是一只鸟并且你杀死了一只老鹰,我希望任务的数量减少,但我还没有找到一种好方法来检查任务的值和被杀死的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为真,则计数应减少。但是我不能让它正常工作,任何人都可以帮忙吗?如果您需要更多信息,请告诉我......

1 个答案:

答案 0 :(得分: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;
}