冷灌注cfscript,开关/壳体之间的范围 [英] coldfusion cfscript, switch/case where case between a range

查看:126
本文介绍了冷灌注cfscript,开关/壳体之间的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我想做三件事之一...
如果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,开关/壳体之间的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆