mercredi 23 septembre 2020

Shortening many if statements in a while loop in Java

How can I shorten many subtractions of the exact same nature in a while loop in Java? I feel like it's very redundant and there can definitely be a shorter way.

while (x != 0) {
        if (x - 100 >= 0) {
            x -= 100;
        }

        if (x - 50 >= 0) {
            x -= 50;
        }

        if (x - 25 >= 0) {
            x -= 25;
        }

        ...

Aucun commentaire:

Enregistrer un commentaire