Looping, I need some tips.

Discussion in 'Plugin Development' started by Innofly, May 30, 2014.

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

    Innofly

    Hello I would like to know if there is a way to make loop to execute something like that but with no limit and cleaner:

    Here is the code:


    Code:java
    1.  
    2. int streak = kills.get(killer.getName());
    3. if(streak == 5 || streak == 15 || streak == 25 || streak == 35 || streak == 45)
    4. {
    5. event.getEntity().getPlayer().sendMessage("You're on a killstreak of " + streak);
    6. }
    7.  
    8.  


    Thanks :)
     
  2. Offline

    Nghtmr9999

    If the streaks are multiples of 5, you could do...

    Code:
    if(streak%5 == 0)
    
    Edit: This could also be any value you want. The modulus operator divides two numbers, but returns the remainder.

    So 5%5 = 0, because 5/5 = 1 with remainder 0.
    4%5 = 4, because 4/5 = 0 with remainder 4.
     
  3. Offline

    Gater12

  4. Offline

    Innofly

    Nghtmr9999 So, if((streak%15) == 1) will replace if(streak == 15 || streak == 30 || streak==45 ...)

    Is that it?
     
  5. Offline

    JBoss925

    No, it's saying if the remainder of streak/15 is 0 then do the code. This can only happen if streak is a multiple of 15

    What you want to do is:

    if(streak%15 == 0)
     
  6. Offline

    Europia79

    Innofly

    Code:java
    1. if ((streak % 5 == 0) && (streak % 10 != 0)) {
    2. event.getEntity().getPlayer().sendMessage("You're on a killstreak of "+ streak);
    3. }


    The second half of the if-statement will never execute unless the first half is true.

    Both halves must be true to execute the sendMessage().

    The first half (streak % 5 == 0) will evalute to true for
    Code:
    5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 ...
    The second half (streak % 10 != 0) will limit it to what you want:
    Code:
    5 15 25 35 45 55 65 75 85 95 105 ...
     
Thread Status:
Not open for further replies.

Share This Page