最近在看手机版连连看游戏源代码,在改进其中一些方法时涉及到了if/else语句,下面是一正确,一错误版本,与大家分享。

 我先把错误的版本贴出来,大家看看错在哪里。

错误版本:

--------------------错误版本:--------------------------------

private boolean twoCornerHorizonalDirectionLeft(Point a, Point b) {
       boolean   hBooleanA=false;
       boolean   hBooleanB=false;
       if (a.x== b.x && a.y == b.y) { // 四条边界的相同点判断
                                     return false;
                                     }
        
       if ((a.y ==0)&&( b.y == 0)) { // 左边界判断
                                     return true;
                                     }
      
       if( a.y == 0){  // A点靠边,判断B点是否有横线。
     for (int y = b.y -1; y >=0; y--) {
         if (map[b.x][y]!= 0) {
         return false;
           }                                                             
                                                      }
                    return true;// 全部为0
                      }// if( a.y = 0)
      
       if(b.y==0){  // B点靠边,判断A点是否有横线。
                    for (int y = a.y -1; y >=0; y--) {
                     if (map[a.x][y]!= 0) {
                      return false;
                     } 
                                           
                    }
                    return true;// 全部为0
                      }// if(b.y=0)
       if(a.y!=0&&b.y!=0) {
                         
                          for (int y = b.y -1; y >=0; y--) {
                           if (map[b.x][y]!= 0) {
                            return false;
                           }                                                             
                          }                         
                          hBooleanA=true;// 全部为0

                         
                          for (int y = a.y -1; y >=0; y--) {
                           if (map[a.x][y] != 0) {
                            return false;
                           }                                                             
                          }
                          hBooleanB = true;// 全部为0
                         
                          return hBooleanA && hBooleanB;
                         
                           }// if( a.y!=0&&b.y!=0 )

    } // twoCornerHorizonalDirectionLeft

--------------------错误版本:--------------------------------

我是在Eclipse中写的这段代码,可是写好后缺提示“此方法必须返回boolean类型的结果”,觉得很奇怪,以为是没有用else的原因,于是就把最后一个分支的判断改成else,结果人歪打正着,通过编译了。以下是错误版本:


 private boolean twoCornerHorizonalDirectionLeft(Point a, Point b) {
       boolean   hBooleanA=false;
       boolean   hBooleanB=false;
       if (a.x== b.x && a.y == b.y) { // 四条边界的相同点判断
                                     return false;
                                     }
        
       if ((a.y ==0)&&( b.y == 0)) { // 左边界判断
                                     return true;
                                     }
      
       if( a.y == 0){  // A点靠边,判断B点是否有横线。
     for (int y = b.y -1; y >=0; y--) {
         if (map[b.x][y]!= 0) {
         return false;
           }                                                             
                                                      }
                    return true;// 全部为0
                      }// if( a.y = 0)
      
       if(b.y==0){  // B点靠边,判断A点是否有横线。
                    for (int y = a.y -1; y >=0; y--) {
                     if (map[a.x][y]!= 0) {
                      return false;
                     } 
                                           
                    }
                    return true;// 全部为0
                      }// if(b.y=0)
       else {
                         
                          for (int y = b.y -1; y >=0; y--) {
                           if (map[b.x][y]!= 0) {
                            return false;
                           }                                                             
                          }                         
                          hBooleanA=true;// 全部为0

                         
                          for (int y = a.y -1; y >=0; y--) {
                           if (map[a.x][y] != 0) {
                            return false;
                           }                                                             
                          }
                          hBooleanB = true;// 全部为0
                         
                          return hBooleanA && hBooleanB;
                         
                           }// if( a.y!=0&&b.y!=0 )

    } // twoCornerHorizonalDirectionLeft

后来经过同事张恒的解释才知道,全写成if的话,编译器就假设认为,所有的情况没有列举完(虽然自己是把所有的情况包括了,但是编译器是假定程序员很笨的前提下设计的)所以才会出现上面的情况,分享一下!