private void B1_Click(object sender, EventArgs e)
        {
            if (d1 !=0M){p1 =d1;}else{p1 =1M;}
            if (d2 !=0M){p2 =d2;}else{p2 =1M;}
            if (d3 !=0M){p3 =d3;}else{p3 =1M;}
            if (d4 !=0M){p4 =d4;}else{p4 =1M;}
            if (d5 !=0M){p5 =d5;}else{p5 =1M;}
            if (d6 !=0M){p6 =d6;}else{p6 =1M;}
            if (d7 !=0M){p7 =d7;}else{p7 =1M;}
            if (d8 !=0M){p8 =d8;}else{p8 =1M;}
            if (d9 !=0M){p9 =d9;}else{p9 =1M;}
            if (d10 !=0M){p10 =d10;}else{p10 =1M;}
            if (d11 !=0M){p11 =d11;}else{p11 =1M;}
            if (d12 !=0M){p12 =d12;}else{p12 =1M;}
            if (d13 !=0M){p13 =d13;}else{p13 =1M;}
            if (d14 !=0M){p14 =d14;}else{p14 =1M;}
            if (d15 !=0M){p15 =d15;}else{p15 =1M;}
            if (d16 !=0M){p16 =d16;}else{p16 =1M;}
            if (d17 !=0M){p17 =d17;}else{p17 =1M;}
            if (d18 !=0M){p18 =d18;}else{p18 =1M;}
            if (d19 !=0M){p19 =d19;}else{p19 =1M;}
            if (d20 !=0M){p20 =d20;}else{p20 =1M;}
            if (d21 !=0M){p21 =d21;}else{p21 =1M;}
            if (d22 !=0M){p22 =d22;}else{p22 =1M;}
            if (d23 !=0M){p23 =d23;}else{p23 =1M;}
            if (d24 !=0M){p24 =d24;}else{p24 =1M;}
            if (d25 !=0M){p25 =d25;}else{p25 =1M;}
            if (d26 !=0M){p26 =d26;}else{p26 =1M;}
            if (d27 !=0M){p27 =d27;}else{p27 =1M;}
            if (d28 !=0M){p28 =d28;}else{p28 =1M;}
            if (d29 !=0M){p29 =d29;}else{p29 =1M;}
            if (d30 !=0M){p30 =d30;}else{p30 =1M;}
            if (d31 !=0M){p31 =d31;}else{p31 =1M;}
            if (d32 !=0M){p32 =d32;}else{p32 =1M;}
            if (d33 !=0M){p33 =d33;}else{p33 =1M;}
            if (d34 !=0M){p34 =d34;}else{p34 =1M;}
            if (d35 !=0M){p35 =d35;}else{p35 =1M;}
            if (d36 !=0M){p36 =d36;}else{p36 =1M;}
            if (d37 !=0M){p37 =d37;}else{p37 =1M;}
            if (d38 !=0M){p38 =d38;}else{p38 =1M;}
            if (d39 !=0M){p39 =d39;}else{p39 =1M;}
            if (d40 !=0M){p40 =d40;}else{p40 =1M;}
            if (d41 !=0M){p41 =d41;}else{p41 =1M;}
            if (d42 !=0M){p42 =d42;}else{p42 =1M;}
            if (d43 !=0M){p43 =d43;}else{p43 =1M;}
            if (d44 !=0M){p44 =d44;}else{p44 =1M;}
            if (d45 !=0M){p45 =d45;}else{p45 =1M;}
            if (d46 !=0M){p46 =d46;}else{p46 =1M;}
            if (d47 !=0M){p47 =d47;}else{p47 =1M;}
            if (d48 !=0M){p48 =d48;}else{p48 =1M;}
            if (d49 !=0M){p49 =d49;}else{p49 =1M;}
            if (d50 !=0M){p50 =d50;}else{p50 =1M;}} d和p都是decimal,所以后面都加M。 我想知道,有什么好办法, 可以减少if...else的使用数量?为了效率和速度了。 太多的if...else判断,肯定效率不高,也不太安全和强健。 

解决方案 »

  1.   

    至少你可以用一下?:操作符吧?呵呵,所以的都可以改为:
    p50 = d50!=0M ? d50: 1M;
      

  2.   

    从设计的角度来说,我觉得你的问题不在于从根本上减少if...else的执行次数,这基本上是无法得到。但你应该做出如下修改:1:你的逻辑结构大体上都是
    if(dX 不等于 0)
    {
        pX = dX;
    }
    else
    {
        pX = 1;
    }
    你应该封装到时一个函数中,然后进行相关的调用,而不是一一写出来,这样你要正确的写出50个行同样的代码才行。这大大增加了出错的机率,而且将来修改逻辑也非常麻烦!
    正确的做法应该是:private static decimal GetDecimal(decimal dX)
    {
        return dX!=0 ?dX:1;
    }p1 = GetDecimal(d1);
    p2 = GetDecimal(d2);
    ...
    2:其中这一点已经表示了,赋值不要在多个地方进行,封装到一个函数中之后,p1只有一处赋值,不会有多处。这样查错也方便。3:至于效率,建议多从其它地方考虑。.NET编译器会自动地优化这些函数的调用。
      

  3.   

    最好是换思路拉,
    那个不管用操作符,或是switch效率都不高的,应该是算法设计上没有提前考虑到现在这种情况
      

  4.   

                decimal[] p=new decimal[50];
                decimal[] d=new decimal[50];
                for (int i = 1; i <= 50; i++)
                {
                    p[i] = d[i] == 0M ? 1M : d[i];
                }
      

  5.   

    使用数组,将这数据存入数组,然后再用for语句循环
      

  6.   

    你是在单击事件中运行此代码的,你的这种效率问题我觉得不用考虑,运行的还是很快的,CPU指令是乱序执行的,并发能力不错,你是不是多虑了?
    如果真想那样,你可以改成if (d24 ==0M){p24 =1M;}else{p24 =d24;} ,
    运行等于零的CPU指令长度和速度好像更快一些.