samedi 1 octobre 2016

Need help making my switch case look nicer

So basically my switch case works but my professor says there are too many returns and "use a variable result, then return it at the end!"

so here is my code

int getMonthValue(){
    switch(month){
        case(1): //January
            if(isLeapYear(year) == true)
                return 6;
            else
                return 0;
        case(2): // February
            if(isLeapYear(year) == true)
                return 2;
            else
                return 3;
        case(3): //March
            return 3;
        case(4): //April
            return 6;
        case(5): //May
            return 1;
        case(6): //June
            return 4;
        case(7): //July
            return 6;
        case(8): //August
            return 2;
        case(9): //September
            return 5;
        case(10): //October
            return 0;
        case(11): //November
            return 3;
        case(12): //December
            return 5;}                                                                 

};

I dont see anything wrong with it, I am sure it could be written better. Would someone be able to show me a way to either format this in a more user friendly way? My professor also wanted me to use breaks int he switch instead, not sure why to use the break over return.

Aucun commentaire:

Enregistrer un commentaire