在switch语句的多个变量在C [英] Multiple variables in switch statement in c

查看:138
本文介绍了在switch语句的多个变量在C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何c。使用switch语句来写下面的语句在C

How to write following statement in c using switch statement in c

int i = 10;
int j = 20;

    if (i == 10 && j == 20)
    {
       Mymethod();
    }
    else if (i == 100 && j == 200)
    {
       Yourmethod();
    }
    else if (i == 1000 || j == 2000) // OR
    {
       Anymethod();
    }

编辑:

我从改变过去的情况下'和'到'或'以后。所以我从这个谁之前编辑回答我的问题的人appologise。

I have changed the last case from 'and' to 'or' later. So I appologise from people who answered my question before this edit.

这情景,例如,我只是想知道,是否有可能或没有。我有google一下,发现它是不可能的,但我相信在计算器大师了。

This scenario is for example, I just wanted to know that is it possible or not. I have google this and found it is not possible but I trust gurus on stackoverflow more.

感谢

推荐答案

在一般你不能。你在做什么已经是好的,虽然你可能想在最后添加一个其他子句赶上意想不到的投入。

In general you can't. What you are doing already is fine, although you might want to add an else clause at the end to catch unexpected inputs.

在特定的实施例似乎j是经常的i值的两倍。如果是一般的规则,你可以尝试做这样的事情而​​不是利用这一点:

In your specific example it seems that j is often twice the value of i. If that is a general rule you could try to take advantage of that by doing something like this instead:

if (i * 2 == j) /* Watch out for overflow here if i could be large! */
{
    switch (i)
    {
        case 10:
            // ...
            break;
        case 100:
            // ...
            break;
        // ...
    }
}

这篇关于在switch语句的多个变量在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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