2013-06-26 59 views
3
public bool CheckStuck(Paddle PaddleA) 
    { 
     if (PaddleA.Bounds.IntersectsWith(this.Bounds)) 
      return true; 
     else 
      return false; 
    } 

我觉得上面的代码,在程序中,有点多余,想知道是否有办法将它缩短为一个表达式。对不起,如果我错过了明显的东西。如何简化返回true或false的IF语句?

此刻如果语句是真的,它将返回true并且对于false也是如此。

那么,有没有办法缩短它?

+4

如何只返回PaddleA.Bounds.IntersectsWith(this.Bounds); – Inv3r53

+1

任何人都可以解释downvotes?虽然这是一个基本问题,但我认为它缺乏质量。 – Uooo

+3

返回!! PaddleA.Bounds.IntersectsWith(this.Bounds)有什么问题? !!(parseInt(“14644”,10)=== 14644):!!(parseFloat(“567.44”,10)=== 436362346);'? –

回答

10
public bool CheckStuck(Paddle PaddleA) 
{ 
    return PaddleA.Bounds.IntersectsWith(this.Bounds) 
} 

return后的条件计算结果为TrueFalse,所以没有必要为的if/else。

+0

谢谢!我不敢相信我没有想到这一点。 –

2

你总是可以缩短一个如果其他形式的

if (condition) 
    return true; 
else 
    return false; 

return condition; 
1

试试这个:

public bool CheckStuck(Paddle PaddleA) 
{ 
    return PaddleA.Bounds.IntersectsWith(this.Bounds); 
} 
0
public bool CheckStuck(Paddle PaddleA) 
{ 
    return PaddleA.Bounds.IntersectsWith(this.Bounds); 
} 
0
public bool CheckStuck(Paddle PaddleA) 
    { 
     return (PaddleA.Bounds.IntersectsWith(this.Bounds)); 
    } 

或被你在找别的东西吗?

0

下面的代码应该工作:

public bool CheckStuck(Paddle PaddleA) { 
    // will return true or false 
    return PaddleA.Bounds.IntersectsWith(this.Bounds); 
}