少于Groovy case / switch语句

时间:2010-05-05 05:02:02

标签: groovy

我有以下开关声明

    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中执行此操作?

2 个答案:

答案 0 :(得分: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";
}

答案 1 :(得分:3)

case { it instanceof Integer && it < 0 }: