增强if-else容错性的方法

       今天写一个苹果移动app的sample时,由于 if-else语句用得不严谨,当程序中出现bug时,影响了排查问题的效率


       出问题的方法是屏幕旋转前触发的事件

       -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

 

      在sample里,需要在屏幕旋转时改变当前的view,左旋右旋时使用另一个view

      最初时代码如下

     


//屏幕旋转时触发的事件
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

   //获取设备屏幕大小

    CGRect devRect = [[UIScreen mainScreen] bounds];
    
    NSUInteger viewHeight = devRect.size.height;
    NSUInteger viewWidth = devRect.size.width;

    
    //屏幕右旋时 ,使用mainLandscapeView 这个view
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight ) {
        self.view  = self.mainLandscapeView; //横屏
        self.view.transform = CGAffineTransformMakeRotation( deg2rad*(-90) );
        self.view.bounds = CGRectMake(0.0, 0.0, viewHeight, viewWidth);

    }

    //屏幕左旋时 ,还是使用mainLandscapeView 这个view

   else if( toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft )
    {
        self.view  = self.mainLandscapeView; //横屏
        self.view.transform = CGAffineTransformMakeRotation( deg2rad*(90) );
        self.view.bounds = CGRectMake(0.0, 0.0, viewHeight,viewWidth );
    }
    //在其他情况下,使用mainPortraitView
    else
    {
        self.view  = self.mainPortraitView ;
        self.view.transform = CGAffineTransformMakeRotation( deg2rad*(0) );
        self.view.bounds = CGRectMake(0.0, 0.0, viewWidth , viewHeight);
    }
 
    [ super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration ];
}


   但其实第三个else不严谨,因为存在屏向上和屏向下两种情况( 在iphone设备里只支持向上,但ipad里支持向上和向下两种情况 )

   倘若在这第三个else里出现问题,将不利于定位到问题


   于是将第三个else改掉,改为

       //屏幕向上
    else if( toInterfaceOrientation == UIInterfaceOrientationPortrait )
    {
        self.view  = self.mainPortraitView ;
        self.view.transform = CGAffineTransformMakeRotation( deg2rad*(0) );
        self.view.bounds = CGRectMake(0.0, 0.0, viewWidth , viewHeight);
    }

    //屏幕向下
    else if( toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown  )
    {
        self.view  = self.mainPortraitView ;
        self.view.transform = CGAffineTransformMakeRotation( deg2rad*(-180) );
        self.view.bounds = CGRectMake(0.0, 0.0, viewWidth , viewHeight);
    }

   //在所有正常情况之外的分支,作为异常处理分支

   else
    {
        ;
    } 


   这样的写法类似switch……case……default, 条件分支清楚,又包含异常处理,倘若在后续的开发中需要添加新的条件分支,最后的异常处理分支 else{;}依然能发挥作用,相比之下,更改前的代码倘若再加上else分支,原先的第三个else 的执行情况就会发生变化,相当于更改了第三个else,这是开发中最不愿看到的事情。


  小结一下:

     写if……else时,要依照 switch……case……default 的风格来写,每一个else 都要加if 条件限制。 最后一个不加 if 的 else 用作default项。处理异常和疏漏

  

这段程序的作用是计算学生的平均成绩,但是在输入学生人数和成绩时进行了错误处理,保证了程序的健壮性。 下面是程序的代码: ```python while True: try: n = int(input('请输入学生人数:')) if n <= 2: print('学生人数太少,必须多于 2 个人。') else: break except: pass aver = 0 for i in range(n): # 这个 while 循环用来保证用户必须输入 0 到 100 之间的数字 while True: try: score = input('请输入第{}个学生的分数:'.format(i+1)) score = float(score) # 把字符串转换为实数 assert 0 <= score <= 100 # 如果成绩不在 [0,100] 则抛出错误 aver += score # 成绩合法累加 break # 输入下一个学生的分数 except: print('分数错误') print('{}个学生的平均成绩为:{:.2f}'.format(n, aver/n)) ``` 程序的思路如下: 1. 使用 `while` 循环,保证输入的学生人数必须大于 2。 2. 在输入学生人数时,使用 `try-except` 结构进行错误处理。如果出现错误,则 `pass` 跳过。 3. 如果学生人数合法,则跳出循环。 4. 开始输入每个学生的成绩,使用 `while` 循环保证输入的成绩必须在 0 到 100 之间。 5. 在输入成绩时,同样使用 `try-except` 结构进行错误处理。如果输入的成绩不在合法范围内,则跳出循环并输出错误信息。 6. 如果输入的成绩合法,则将其累加到平均成绩中。 7. 最后输出学生的平均成绩。 这段程序使用了异常处理和断言语句,增强了程序的健壮性,能够有效地避免输入错误数据导致程序崩溃的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值