0 在 switch 情况下? [英] 0 in switch case?

查看:36
本文介绍了0 在 switch 情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,可能是个愚蠢的问题,但是当我有一些值为 0(零)的 int 变量时,我遇到了一个巨大的问题.

Sorry for maybe dumb question, but i have HUGE problem with one case when i have some int variable with value of 0(zero).

            switch ($starost_vozila){
                case (0):
                    switch ($podaci['tip_motora']){
                        case ("motor1"):
                            $eko_taksa = 485;
                        break;
                        case ("motor2"):
                            $eko_taksa = 243;
                        break;
                        case ("motor3"):
                            $eko_taksa = 121;
                        break;
                        case ("motor4"):
                            $eko_taksa = 194;
                        break;
                    }
                break;
                case ($starost_vozila < 6):
                    switch ($podaci['tip_motora']){
                        case ("motor1"):
                            $eko_taksa = 485;
                        break;
                        case ("motor2"):
                            $eko_taksa = 243;
                        break;
                        case ("motor3"):
                            $eko_taksa = 121;
                        break;
                        case ("motor4"):
                            $eko_taksa = 194;
                        break;
                    }
                break;
                case ($starost_vozila > 5 && $starost_vozila < 11):
                    switch ($podaci['tip_motora']){
                        case ("motor1"):
                            $eko_taksa = 667;
                        break;
                        case ("motor2"):
                            $eko_taksa = 273;
                        break;
                        case ("motor3"):
                            $eko_taksa = 136;
                        break;
                        case ("motor4"):
                            $eko_taksa = 218;
                        break;
                    }
                break;

切换继续更多,但这是我的问题,在这段代码中.

Switch continue more, but here is my problem, in this piece of code.

如果我不输入case (0):"并使用这个:

If i dont put "case (0):" and use this:

case ($starost_vozila >= 0 && $starost_vozila < 6):

然后下一个案例会以某种方式激活并打印出$eko_taksa = 667;".

Then the case that is next will somehow become active and it will print out that "$eko_taksa = 667;".

当$starost_vozila = 0"时,这就是所有问题,但当它是任何小于 6 的数字时,上述情况都有效.

That is all problem when "$starost_vozila = 0" but when it is any number less then 6 than this case above works.

这里的每个 var 都是 int.一切正常,除了$starost_vozila = 0"和当我使用case ($starost_vozila >= 0 && $starost_vozila <6):".

Every var here is int. Everything works ok except when "$starost_vozila = 0" and when i use "case ($starost_vozila >= 0 && $starost_vozila < 6):".

我不知道发生了什么......哦

I have no idea what is going on... Oo

对不起,如果这是一个愚蠢的问题.:(

Sorry if this is dumb question. :(

推荐答案

switch case 不接受需要评估的语句.它们采用简单的字符串、布尔值或数字进行比较

switch cases don't take statements that need to be evaluated. They take simple strings, booleans or numbers to be compared against

所以说你有

$x = 0

switch( $x ) {
    case( $x < 10 ):
    ...
    break;
}

您希望该案例运行,但它没有运行,这就是原因

you are expecting that case to run but it doesn't and here's why

( $x <10 ) 计算结果为 true所以你实际上拥有的是:

( $x < 10 ) evaluates to true so what you actually have is:

$x = 0

switch( $x ) {
    case true: //!!!
    ...
    break;
}

0 != true 所以案例失败

你需要使用

if() {

} else if() {

} else {

}

这篇关于0 在 switch 情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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