在 groovy case 语句中使用范围

Using a range in a groovy case statement

我正在编写 Jenkins 管道,我想使用 groovy switch 语句来匹配 int 值的范围,这样我就不必为每个数字都写一个 case范围。不习惯 groovy,如果这是一个简单的问题,我们深表歉意。例如(不工作):

switch (diskuse) {
   case 1-5: //this doesn't work
     println('disk use is under 50')
     break

   case [5-9]: //this also doesn't work
     println('disk use is OVER 50!')
     break

   default: //the switch always hits this case
     println('no disk use info available')
}

正确的范围文字看起来像 Groovy 中的 1..5
您的开关操作应如下所示:

switch (diskuse) {
   case 1..5: //inclusive range
     println('disk use is under 50')
     break

   case 5..<9: //exclusive range, 9 is exluded
     println('disk use is OVER 50!')
     break

   default:
     println('no disk use info available')
}