Search code examples
javaandroidmultiplication

Android - How to simplify multiple of ten?


I want to multiple values like this:

if (currentScore == 10 | currentScore == 20 | currentScore == 30 | currentScore == 40 | currentScore == 50 | currentScore == 60
            | currentScore == 70 | currentScore == 80 | currentScore == 90 | currentScore == 100 | currentScore == 110
            | currentScore == 120 | currentScore == 130 | currentScore == 140 | currentScore == 150 | currentScore == 160
            | currentScore == 170 | currentScore == 180 | currentScore == 190 | currentScore == 200) {
        editor.putInt("TOP_LEVEL", topLevel + 1);
        editor.apply();
    }

How to simplify that code so can count many currentScore. Thanks


Solution

  • Use the Java % operator:

    if (currentScore % 10 == 0) {
        editor.putInt("TOP_LEVEL", topLevel + 1);
        editor.apply();
    }
    

    The % operator returns a remainder. If the remainder of the division of "currentScore" by 10 is 0, this means that currentScore is an exact multiple of 10.