从枚举中获取一定数量

时间:2015-07-10 00:35:30

标签: java enums

我有一个枚举,我想从中获取一些信息。 这是枚举:

        $parameters = array(
            'filters' => 'ga:pagePath==/about'
            'dimensions' => 'ga:pagePath',
            'metrics' => 'ga:pageviews,ga:uniquePageviews',
            'sort' => '-ga:pageviews'
        );

        $data = self::$analytics->data_ga->get(
            'ga:100174927',
            date("Y-m-d", $start),
            date("Y-m-d", $finish),
            'ga:visits',
            $parameters
        );

        $analytics = array(
            'pageviews'       => 0,
            'uniquePageviews' => 0
        );

        // This is the foreach loop that is creating the error:

        foreach ($data->getRows() as $key => $row)
        {    
            $analytics['pageviews']       += $row[1];
            $analytics['uniquePageviews'] += $row[2];
        }

        return $analytics;

以下是一些代码,可以了解所有内容的含义:

public static enum Prayer {
        THICK_SKIN(new double[] {21233, 1, 83, 12}, DEFENCE_PRAYER, false),
        BURST_OF_STRENGTH(new double[] {21234, 4, 84, 12}, STRENGTH_PRAYER, false),
        CLARITY_OF_THOUGHT(new double[] {21235, 7, 85, 12}, ATTACK_PRAYER, false),
        ROCK_SKIN(new double[] {21236, 10, 86, 6}, DEFENCE_PRAYER, false),
        SUPERHUMAN_STRENGTH(new double[] {21237, 13, 87, 6}, STRENGTH_PRAYER, false),
        IMPROVED_REFLEXES(new double[] {21238, 16, 88, 6}, ATTACK_PRAYER, false),
        RAPID_RESTORE(new double[] {21239, 19, 89, 26}, -1, false),
        RAPID_HEAL(new double[] {21240, 22, 90, 18}, -1, false),
        PROTECT_ITEM(new double[] {25020, 25, 91, 18}, -1, false),
        STEEL_SKIN(new double[] {21242, 28, 92, 3}, DEFENCE_PRAYER, false),
        ULTIMATE_STRENGTH(new double[] {21243, 31, 93, 3}, STRENGTH_PRAYER, false),
        INCREDIBLE_REFLEXES(new double[] {21244, 34, 94, 3}, ATTACK_PRAYER, false),
        PROTECT_FROM_MAGIC(new double[] {21245, 37, 95, 3, 2}, OVERHEAD_PRAYER, false),
        PROTECT_FROM_MISSILES(new double[] {21246, 40, 96, 3, 1}, OVERHEAD_PRAYER, false),
        PROTECT_FROM_MELEE(new double[] {21247, 43, 97, 3, 0}, OVERHEAD_PRAYER, false),
        RETRIBUTION(new double[] {2171, 46, 98, 1, 3}, OVERHEAD_PRAYER, false),
        REDEMPTION(new double[] {2172, 49, 99, 2, 5}, OVERHEAD_PRAYER, false),
        SMITE(new double[] {2173, 52, 100, 2, 4}, OVERHEAD_PRAYER, false),
        SHARP_EYE(new double[] {70080, 8, 601, 12}, RANGE_PRAYER | ATTACK_PRAYER | STRENGTH_PRAYER, false),
        MYSTIC_WILL(new double[] {70082, 9, 602, 12}, MAGIC_PRAYER | ATTACK_PRAYER | STRENGTH_PRAYER, false),
        HAWK_EYE(new double[] {70084, 26, 603, 6}, RANGE_PRAYER | ATTACK_PRAYER | STRENGTH_PRAYER, false),
        MYSTIC_LORE(new double[] {70086, 27, 604, 6}, MAGIC_PRAYER | ATTACK_PRAYER | STRENGTH_PRAYER, false),
        EAGLE_EYE(new double[] {70088, 44, 605, 3}, RANGE_PRAYER | ATTACK_PRAYER | STRENGTH_PRAYER, false),
        MYSTIC_MIGHT(new double[] {70090, 45, 606, 3}, MAGIC_PRAYER | ATTACK_PRAYER | STRENGTH_PRAYER, false),
        CHIVALRY(new double[] {70092, 60, 607, 1.5}, ATTACK_PRAYER | STRENGTH_PRAYER | DEFENCE_PRAYER, false),
        PIETY(new double[] {70094, 70, 608, 1.5}, ATTACK_PRAYER | STRENGTH_PRAYER | DEFENCE_PRAYER, false),
        PROTECT_ITEM_(new double[] {87231, 50, 83, 18, 12567, 2213}, -1, true),
        SAP_WARRIOR(new double[] {87233, 50, 84, 2.57}, SAP_PRAYER, true),
        SAP_RANGER(new double[] {87235, 52, 85, 2.57}, SAP_PRAYER, true),
        SAP_MAGE(new double[] {87237, 54, 101, 2.57}, SAP_PRAYER, true),
        SAP_SPIRIT(new double[] {87239, 56, 102, 2.57}, SAP_PRAYER, true),
        BERSERKER(new double[] {87241, 59, 86, 18, 12589, 2266}, -1, true),
        DEFLECT_SUMMON(new double[] {87243, 62, 87, 3}, OVERHEAD_PRAYER, true),
        DEFLECT_MAGIC(new double[] {87245, 65, 88, 3, 10}, OVERHEAD_PRAYER, true),
        DEFLECT_RANGE(new double[] {87247, 68, 89, 3, 11}, OVERHEAD_PRAYER, true),
        DEFLECT_MELEE(new double[] {87249, 71, 90, 3, 9}, OVERHEAD_PRAYER, true),
        LEECH_ATTACK(new double[] {87251, 74, 91, 3}, LEECH_PRAYER, true),
        LEECH_RANGE(new double[] {87253, 76, 103, 3}, LEECH_PRAYER, true),
        LEECH_MAGE(new double[] {87255, 78, 104, 3}, LEECH_PRAYER, true),
        LEECH_DEFENCE(new double[] {88001, 80, 92, 3}, LEECH_PRAYER, true),
        LEECH_STRENGTH(new double[] {88003, 82, 93, 3}, LEECH_PRAYER, true),
        LEECH_ENERGY(new double[] {88005, 84, 94, 3.6}, LEECH_PRAYER, true),
        LEECH_SPECIAL(new double[] {88007, 86, 95, 3}, LEECH_PRAYER, true),
        WRATH(new double[] {88009, 89, 96, 12, 16}, OVERHEAD_PRAYER, true),
        SOUL_SPLIT(new double[] {88011, 92, 97, 2, 17}, OVERHEAD_PRAYER, true),
        TURMOIL(new double[] {88013, 95, 105, 2, 12565, 2226}, SAP_PRAYER | LEECH_PRAYER, true);

我想每次都获得第四个号码。它必须采用这样的形式,但这不起作用:

private Prayer(double[] data, int prayerMask, boolean curse) {
            this.id = (int) data[0];
            this.level = (int) data[1];
            this.config = (int) data[2];
            this.drain = data[3] * 2;
            this.icon = (int) (data.length == 5 ? data[4] : -1);
            this.anim = data.length == 6 ? (int) data[4] : -1;
            this.graphic = data.length == 6 ? (int) data[5] : -1;
            this.name = Misc.formatPlayerName(this.toString());
            this.prayMask = prayerMask;
            this.curse = curse;
        }
...
public double getDrain() {
            return drain;
        }

说明:我确实想要发生的是public void handlePrayerDrain() { double toRemove = 0.0; for (int j = 0; j < prayerData.length; j++) { if (c.prayerActive[j]) { toRemove += prayerData[j]/20; } } if (toRemove > 0) { toRemove /= (1 + (0.035 * c.playerBonus[11])); } c.prayerPoint -= toRemove; if (c.prayerPoint <= 0) { c.prayerPoint = 1.0 + c.prayerPoint; reducePrayerLevel(); } } 方法获取枚举的长度(handlePrayerDrain),之后它会检查哪一个(for (int j = 0; j < prayerData.length; j++) {)和之后,它检查排水量是多少(if (c.prayerActive[j]) {

这是我的Client.java中的一些代码(如果这有帮助):

toRemove += prayerData[j]/20;

2 个答案:

答案 0 :(得分:1)

如果要迭代枚举的所有可能值,请尝试:

for (Prayer p : Prayer.values()) {
    // your logic goes here
}

答案 1 :(得分:0)

如果您使用的是Java 8,那么流提供了一种相当简洁的方法来汇总enum中的某些值。虽然要说明你想要做什么有点难以理解,但这里有一个类似的例子可能会给你你想要的东西(我想: - ))。

enum Prayer {
    ...
    public double getCost();
}

class Player {
    ...
    public boolean hasActivePrayer(Prayer prayer);
}

double toRemove = Arrays.stream(Prayer.values())
    .filter(player.getActivePrayer()::contains)
    .mapToDouble(Prayer::getDrain)
    .sum() / 10;

...