lundi 27 septembre 2021

Avoiding if/else chain when switch statement is not available in Java

This is most likely a redundant question so I apologize if that may be; I tried looking for something specific to this but it wasn't quite what I was looking for.

I need a way to avoid this following if/else chain, if there isn't one that's fine. It uses a float type so it cannot be switched.

if (luminance <= 0.5 && luminance >= 0.48) {
                            fi.print("B");
                        } else if (luminance <= 0.48 && luminance >= 0.46) {
                            fi.print("%");
                        } else if (luminance <= 0.46 && luminance >= 0.44) {
                            fi.print("$");
                        } else if (luminance <= 0.44 && luminance >= 0.42) {
                            fi.print("#");
                        } else if (luminance <= 0.42 && luminance >= 0.4) {
                            fi.print("@");
                        }
 

I assume there's some sort of loop that can fix this. This chunk of code goes down by 0.02 all the way down to 0.1

Ignore the other variables: fi and luminance.

Thank you very much for your help!

Aucun commentaire:

Enregistrer un commentaire