为什么一个变量与许多值的不相等检查总是返回true?

为什么一个变量与许多值的不相等检查总是返回true?

我有一个变量v在我的节目里,它可能需要任何值集合中的值。

"a", "b", "c", ..., "z"

我的目标是只在以下情况下执行一些语句v不是"x""y",或"z".

我试过了

  • 对于类C语言(其中相等运算符比较实际字符串值C#JavaScriptPHP)

    if (v != "x" || v != "y" || v != "z"){
        // the statements I want to be executed
        // if v is neither "x", nor "y", nor "z"}
  • 用于类似PASCAL的语言(例如:plsql)

    IF (v != 'x' OR v != 'y' OR v != 'z') THEN
        -- the statements I want to be executed
        -- if v is neither "x", nor "y", nor "z"END IF;

if条件内的语句总是被执行..我做错什么了吗?


扬帆大鱼
浏览 694回答 3
3回答

炎炎设计

使用&&/AND/and,不是||/OR/or:v != "x" && v != "y" && v != "z"问题如果if块,则为if块的条件。总是评估为true..逻辑表达式一定是错的。让我们考虑一下v != "x" || v != "y" || v != "z"的每一个值v.什么时候v = "x",v != "x"成"x" != "x",也就是假的. v != "y"成"x" != "y",也就是千真万确. v != "z"成"x" != "y",也就是千真万确.表达式的计算结果为false || true || true,也就是千真万确.什么时候v = "y",表达式变成"y" != "x" || "y" != "y" || "y" != "z"或true || false || true,也就是千真万确.什么时候v = "z",表达式变成"z" != "x" || "z" != "y" || "z" != "z"或true || true || false,也就是千真万确.的任何其他价值v,表达式的计算结果为true || true || true,也就是千真万确.或者,考虑一下真值表:       │     A          B          C      │   v    │  v != "x"   v != "y"   v != "z"  │  A || B || C ───────┼──────────────────────────────────┼──────────────  "x"   │    false      true       true    │     true  "y"   │    true       false      true    │     true  "z"   │    true       true       false   │     true other  │    true       true       true    │     true如您所见,您的逻辑表达式总评估为true.解您要做的是,找到一个计算为true什么时候(v is not "x")and(v is not "y")and(v is not "z").正确的结构是,用于C类语言(如。C#, JavaScript-(可能需要严格的相等操作符!==), PHP)if (v != "x" && v != "y" && v != "z"){     // the statements I want to be executed     // if v is neither "x", nor "y", nor "z"}类帕斯卡语言plsqlIF (v != 'x' AND v != 'y' AND v != 'z') THEN     -- the statements I want to be executed     -- if v is neither "x", nor "y", nor "z"END IF;德摩根定律通过德摩根定律,表达式也可以重写为(使用C语法)!(v == "x" || v == "y" || v == "z")意义not((v is "x")or(v is "y")or(v is "z")).这使得逻辑更加明显。特定语言有些语言有特定的结构来测试集合中的成员资格,或者可以使用数组/列表操作。SQL: v NOT IN ('x', 'y', 'z')JavaScript: ["x", "y", "z"].indexOf(v) == -1Python: v not in {"x", "y", "z"}爪哇: Arrays.asList("x", "y", "z").contains(v)爪哇-9(及以上):Set.of("x", "y", "z").contains(v)

海绵宝宝撒

我想我会为Bourneshell脚本提供一个答案,因为语法有点奇怪。传统/POSIXsh字符串相等测试是[命令(是的,这是一个不同的命令名!)在报价等方面有一些令人讨厌的要求。#### WRONG if [ "$v" != 'x' ] || [ "$v" != 'y'] || [ "$v" != 'z' ]; then     : some code which should happen when $v is not 'x' or 'y' or 'z' fi现代贝壳如Ksh,Bash,Zsh等也有[[有点不那么烦人。#### STILL WRONG if [[ $v != 'x' || $v != 'y' || $v != 'z' ]]; then     :  some code which should happen when $v is not 'x' or 'y' or 'z' fi我们应该强调在每个令牌周围有空格的要求,这是许多初学者忽略的事情(也就是说,您不能说if[[$v或$v!='y'在命令和运算符周围没有空格),以及表观引用的可选性。不引用值通常不是句法错误,但它会导致严重的不期望。语义化如果你没有引用一个需要被引用的价值,那就麻烦了。这里最明显的解决办法是使用&&而不是||但你也应该注意到[[通常对正则表达式的运动支持,所以您可以这样说if [[ ! $v =~ ^(x|y|z)$ ]]; then     : yeah fi别忘了那个可靠的老人case这是非常自然的说法,并可移植到1970年代后期:case $v in     x | y | z)        ;; # don't actually do anything in this switch     *) # anything else, we fall through to this switch        yeah        some more yeah        in fact, lots of yeah;;  esac拖着的双分号起初会导致动脉瘤,但你很快就会恢复,学会欣赏,甚至喜欢它们。POSIX允许在匹配表达式之前放置一个括号,这样就没有未配对的右括号,但这种用法并不常见。

慕无忌1623718

对于PHP,您可以使用这样的东西:if(strpos('xyz',$v[0])===false)//example 1//strpos returns false when the letter isn't in the string//returns the position (0 based) of the substring//we must use a strict comparison to see if it isn't in the substringif(!in_array($v[0],array('x','y','z')))//example 2//example 3$out=array('x'=>1,'y'=>1,'z'=>1); //create an arrayif(!$out[$v[0]]) //check if it's not 1if(!preg_match('/^[xyz]$/',$v))//example 4, using regexif(str_replace(array('x','y','z'),'',$v[0]))//example 5if(trim($v[0],'xyz'))//example 6对于Javascript:if(~'xyz'.search(v[0]))//example 1(.indexOf() works too)if(!(v[0] in {x:0,y:0,z:0}))//example 2if(~['x','y','z'].indexOf(v[0]))//example 3, incompatible with older browsers.if(!/^[xyz]$/.match(v))//example 4if(v.replace(/^[xyz]$/))//example 5对于MySQL:Select not locate(@v,'xyz'); -- example 1select @v not in ('x','y','z'); -- example 2-- repetition of the same pattern for the othersC组:if(!strstr('xyz',v))//example 1, untested有更多的方法,我就是太懒了。发挥你的想象力,只需写出你更喜欢的!
打开App,查看更多内容
随时随地看视频慕课网APP