Need Help with percentual math!

Discussion in 'Plugin Development' started by Gabum, Jun 2, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Gabum

    Hello!

    I'm currently working on a Custom Battle System.

    Every Mob has a maximum health. For our example, it's 5.

    Then, every mob has a current health, which is also stored in a HashMap.

    I want to display the left health of the mob using the players' hearts. So I'm using the following code:

    Code:java
    1. p.sendMessage("Current Lifes:" + MonsterLifes.get(l));
    2. p.sendMessage("Maximum Lifes:" + MaximumMonsterlifes.get(l.getCustomName()).toString());
    3. double PlayerHealth = MonsterLifes.get(l) / MaximumMonsterlifes.get(l.getCustomName()) * 20;
    4. p.sendMessage(PlayerHealth + " left.");


    And the Output is the following (I'm using a weapon that deals 2 damage) after 2 hits:

    Current Lifes: 3
    Maximum Lifes: 5
    0.0 left.

    Current Lifes: 1
    Maximum Lifes: 5
    0.0 left.

    The thing that is confusing me right now: Why the hell is the quotient of Monsterlifes divided by MaximumMonsterLifes always 0.0? How can I fix this?
     
  2. Offline

    Necrodoom

    int / int results in int.
    Cast one of these to double first.
     
  3. Offline

    FlareLine

    Gabum I'm just wondering why you would need to divide the two variables...
    Wouldn't you just subtract the current from the maximum?
     
  4. Offline

    Gabum

    FlareLine
    I need to get the percentual value of the both, which I then multiplied by twenty to get the health I need to fill in.

    Necrodoom
    Thank you very much, that helped!
     
  5. Offline

    FlareLine

    Gabum This is not the inexperienced Bukkit Developer you're looking for... *shifty eyes*
     
  6. Offline

    Necrodoom

    What?
     
    ZodiacTheories and Garris0n like this.
Thread Status:
Not open for further replies.

Share This Page