双或单开关

发布于 2024-11-30 04:13:09 字数 1490 浏览 0 评论 0原文

好吧,这可能是一个愚蠢的问题,但我无法找到答案:

情况很简单,我有一个函数,其条目中有一个整数,并且有两个变量要根据该整数进行分配。问题是分配给变量的值在某些情况下是通用的,但这些情况对于两个变量来说并不相同。 (如果还不够清楚,请参阅示例)。

我想知道这种情况的最佳做法是什么。 是否类似于 :

function test(a){
    var x,y;
    switch (a){
        case 0:
        case 1:
        case 7:
            y=...;
            break;
        case 2:
        case 6:
            y=...;
            break;
        case 3:
        case 4:
        case 5:
            y=...;
    }
    switch(a){
        case 5:
        case 6:
        case 7:
            x=...;
            break;
        case 0:
        case 4:
            x=...;
            break;
        case 1:
        case 2:
        case 3:
            x=...;
    }
    ...
}

function test(a){
    var x,y;
    switch (a){
        case 0:
            x=...;
            y=...;
            break;              
        case 1:
            x=...;
            y=...;
            break;
        case 2:
            x=...;
            y=...;
            break;
        case 3:
            x=...;
            y=...;
            break;
        case 4:
            x=...;
            y=...;
            break;
        case 5:
            x=...;
            y=...;
            break;
        case 6:
            x=...;
            y=...;
            break;
        case 7:
            x=...;
            y=...;
    }
    ...
}

Or 将两者混合使用,并在每种情况下将值分配给 x 并为 y 的值进行分组?

请注意,可能有超过 8 个值,这只是为了示例。 提前致谢。

Well it might be a dumb question, but I'm unable to find an answer:

The case is simple, I have a function with an integer in entry and two variable to assign depending of that. The problem is that the value assigned to the variables is common to certain case, but those case are not the same for the two variables. (if it's not clear enough, see the example).

I was wondering what was the best practice for such a case.
Is it something like :

function test(a){
    var x,y;
    switch (a){
        case 0:
        case 1:
        case 7:
            y=...;
            break;
        case 2:
        case 6:
            y=...;
            break;
        case 3:
        case 4:
        case 5:
            y=...;
    }
    switch(a){
        case 5:
        case 6:
        case 7:
            x=...;
            break;
        case 0:
        case 4:
            x=...;
            break;
        case 1:
        case 2:
        case 3:
            x=...;
    }
    ...
}

or

function test(a){
    var x,y;
    switch (a){
        case 0:
            x=...;
            y=...;
            break;              
        case 1:
            x=...;
            y=...;
            break;
        case 2:
            x=...;
            y=...;
            break;
        case 3:
            x=...;
            y=...;
            break;
        case 4:
            x=...;
            y=...;
            break;
        case 5:
            x=...;
            y=...;
            break;
        case 6:
            x=...;
            y=...;
            break;
        case 7:
            x=...;
            y=...;
    }
    ...
}

Or to use a mix of the two with the value assigned to x in each case and to make groups for the value of y?

Note that there might be more than 8 value, it's just for the example.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2024-12-07 04:13:09

如果 switch 语句中的不同情况之间没有真正的重叠,那么最好将它们分开。

如果操作之间存在大部分共性,您可以将它们与各个 case 块中的某些特殊情况组合起来,但您似乎表明这里的情况并非如此。

但是,如果这不仅仅是一个更复杂情况的简单示例,您可以通过以下方式实现更紧凑的解决方案:

//                     index:  0  1  2  3  4  5  6  7  8  9 10 11 12
static const int lookupX[] = { 2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9};
static const int lookupY[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9};
if ((a >=0) && (a < sizeof(lookupX) / sizeof(*lookupX)))
    x = lookupX[a];
if ((a >=0) && (a < sizeof(lookupY) / sizeof(*lookupY)))
    y = lookupY[a];

这将允许您将值保留在更小的“配置”中”部分,以便您可以轻松了解意图。范围检查和查找变得非常简单。

该代码是针对 C 定制的 - 我不确定您使用的是哪种特定语言(因为 var 语句),但它基本上只在索引有效时进行查找。您需要将该部分翻译成您选择的语言。

If there's no real overlap between the different cases in the switch statements, you're probably better off having them separate.

If there was a majority of commonality between the actions, you could combine them with certain special cases within the individual case blocks but you seem to indicate that's not the case here.

However, if this isn't just a simple example of a much more complex case, you can achieve a more compact solution with something like:

//                     index:  0  1  2  3  4  5  6  7  8  9 10 11 12
static const int lookupX[] = { 2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9};
static const int lookupY[] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9};
if ((a >=0) && (a < sizeof(lookupX) / sizeof(*lookupX)))
    x = lookupX[a];
if ((a >=0) && (a < sizeof(lookupY) / sizeof(*lookupY)))
    y = lookupY[a];

This will allow you to keep the values in a much smaller "configuration" section so that you can easily see the intent. The range checking and lookup then become very simple.

That code is tailored to C - I'm not sure what specific language you're using (because of the var statement) but it's basically only doing the lookup if the index will be valid. You'll need to translate that bit to your language of choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文