2013-06-26 62 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); 
}