问题描述
我有一个问题,我想做三件事之一...
如果x的值是1-5(包括)do A,如果x在6-13之间(包括) do B,如果x在14-16之间,那么C。
I've got a problem where I want to do one of three things...if the value of x is 1-5 (inclusive) do A, if x is between 6-13 (inclusive) do B, and if x is between 14-16 do C.
我认为开关的情况会确定,虽然我想我可以使用一个简单的IF /但是,当我编码它,我不禁想到有一个更优雅的方式说明这使用开关/ case(只是为了防止我遇到类似的需要,有三个选项)。
I figured the switch case would be ok, although I guess I could use a plain IF / ELSE IF, however, as I coded it, I can't help but think there is a more elegant way of stating this USING the switch/case (just in case I encounter a similar need that has more then three options).
这里是我有的:
switch ( x ) {
case 1:case 2:case 3:case 4:case 5:
// DO A
break;
case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:
// DO B
break;
case 14:case 15:case 16:
// DO C
break;
}
有一种方法可以指定between )?
is there a way in the case to specify "between" (inclusive or exclusive)?
感谢
推荐答案
Switch语句设计为使用单一。除非,否则唯一的选项是您已经写入的选项OR使用 if / else if / else
,AFAIK。在大多数情况下,后者比一堆硬编码的 case
语句IMO更清洁。
Nope. Switch statement are designed to work with single, constant values. Unless the comparison is such that the value can be modified to conform to that rule, the only options are what you have written already OR using if/else if/else
, AFAIK. In most cases, the latter is cleaner than a bunch of hard coded case
statements IMO.
这篇关于冷灌注cfscript,开关/壳体之间的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!