2010-05-05 88 views
6

少,我有以下switch語句比在Groovy情況/ switch語句

switch (points) { 
     case 0: name = "new"; break; 
     case 1..14: badgeName = "bronze-coin"; break; 
     case 15..29: badgeName = "silver-coin"; break; 
     default: badgeName = "ruby"; 
    } 

我想第一種情況下(0的情況),包括我怎樣才能做到這點小於或等於0。在Groovy?

回答

6
switch(points) 
{ 
    case Integer.MIN_VALUE..0: badgeName = "new"; break; 
    case 1..14: badgeName = "bronze-coin"; break; 
    case 15..29: badgeName = "silver-coin"; break; 
    default: badgeName = "ruby"; 
} 
3
case { it instanceof Integer && it < 0 }: 
+1

it <= 0肯定嗎? – 2010-05-05 06:05:46

+0

@Tim - 那不起作用 – 2010-05-05 07:40:58

+0

Doh!錯過了第一個案例: - / – 2010-05-05 07:43:56