mardi 12 juillet 2016

JAVA - More Efficient Way to Do This (If/else branch, variable assignment)

The variable "varOptions" is attained via scanner. Basically, I want: [A, B, C, D, E, F] to become: [0, 1, 2, 3, 4, 5]. It is also very important that the code remain non-case sensitive. Currently, this code is rather bulky. I'm wondering if there is a way to make this more efficient. Thanks in advance!

    if ("A".equalsIgnoreCase(varOptions)) {
        int varOpt = 0;
    }
    else if ("B".equalsIgnoreCase(varOptions)) {
        int varOpt = 1;
    }
    else if ("C".equalsIgnoreCase(varOptions)) {
        int varOpt = 2;
    }
    else if ("D".equalsIgnoreCase(varOptions)) {
        int varOpt = 3;
    }
    else if ("E".equalsIgnoreCase(varOptions)) {
        int varOpt = 4;
    }
    else if ("F".equalsIgnoreCase(varOptions)) {
        int varOpt = 5;
    }

Aucun commentaire:

Enregistrer un commentaire