Get rid of ugly if statements

后端 未结 25 1973
借酒劲吻你
借酒劲吻你 2020-12-02 05:59

I have this ugly code:

if ( v > 10 ) size = 6;
if ( v > 22 ) size = 5;
if ( v > 51 ) size = 4;
if ( v > 68 ) size = 3;
if ( v > 117 ) size = 2         


        
相关标签:
25条回答
  • 2020-12-02 06:42
                if (v <= 10)
                    return size;
                else {
                    size = 1;
    
                    if (v > 145)
                        return size;
                    else if (v > 117)
                        return ++size;
                    else if (v > 68)
                        return (size+2);
                    else if (v > 51)
                        return (size+3);
                    else if (v > 22)
                        return (size+4);
                    else if (v > 10)
                        return (size+5);
                }
    

    This will execute the necessary if statements only.

    0 讨论(0)
提交回复
热议问题