jeudi 26 mai 2016

Is there anything like switchcase for range of values? [duplicate]

This question already has an answer here:

I need to change my block color according to the x value, which influance by the i value, more generally- check in which range a number is. I know that if I would like to check if the number is one of some specific options I can use Switch-case, but is there any better way to check in which range the number is?

Currently this is my code

for (int i = 0; i < 15; i++) {
        if (i < 2)
            color = Color.red;
        else if (i < 4)
            color = Color.orange;
        else if (i < 6)
            color = Color.yellow;
        else if (i < 9)
            color = Color.green;
        else if (i < 11)
            color = Color.blue;
        else if (i < 13)
            color = Color.pink;
        else
            color = Color.cyan;
        listOfBlocks.add(new Block(53.1 * i + 0.5, 250, 52, 20, 1, color));
    }

thanks

Aucun commentaire:

Enregistrer un commentaire