为什么针对多个值对一个变量进行非等式检查总是返回 true? [英] Why does non-equality check of one variable against many values always return true?

查看:23
本文介绍了为什么针对多个值对一个变量进行非等式检查总是返回 true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个变量 v,它可以从一组值中取任何

I have a variable v in my program, and it may take any value from the set of values

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

我的目标是仅当 v 不是 "x""y" 时才执行某些语句>z".

And my goal is to execute some statement only when v is not "x", "y", or "z".

我试过了,

  • 对于类 C 语言(其中相等运算符比较实际字符串值;例如 , , )

  • for C-like languages (where equality operators compare the actual string values; e.g. 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"
}

  • 对于类似 Pascal 的语言(例如 )

    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 条件中的语句总是被执行.我做错了什么吗?

    The statements inside the if condition always get executed. Am I doing anything wrong?

    推荐答案

    使用 &&/AND/and,而不是 <代码>||/OR/or:

    Use &&/AND/and, not ||/OR/or:

    v != "x" && v != "y" && v != "z"
    

    问题

    如果 if 块总是被执行,则 if 块的条件 总是评估为 true.逻辑表达式一定是错误的.

    Problem

    If an if block is always executed, the condition for the if block always evaluates to true. The logical expression must be wrong.

    让我们考虑 v != x"||v != y"||v != "z" 对于 v 的每个值.

    Let us consider v != "x" || v != "y" || v != "z" for each value of v.

    • v = "x" 时,

    v != "x" 变成 "x";!= "x",这是 false.
    v != y" 变成 x";!= "y",这是 true.
    v != "z" 变成 "x";!= "z",这是.

    v != "x" becomes "x" != "x", which is false.
    v != "y" becomes "x" != "y", which is true.
    v != "z" becomes "x" != "z", which is true.

    表达式计算结果为 false ||真||真,即.

    The expression evaluates to false || true || true, which is true.

    v = "y"时,表达式变为

      "y" != "x" || "y" != "y" || "y" != "z"
    

    true ||假||真,即.

    v = "z"时,表达式变为

      "z" != "x" || "z" != "y" || "z" != "z"
    

    true ||真||假,这是.

    对于 v 的任何其他值,表达式的计算结果为 true ||真||真,即.

    For any other value for v, the expression evaluates to true || true || true, which is 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.

    As you can see, your logical expression always evaluates to true.

    你想要做的是,找到一个逻辑表达式,当

    What you want to do is, find a logical expression that evaluates to true when

    (v 不是x")and(v 不是y")and(v 不是z").

    正确的构造是,

    • 对于类 C 语言(例如 , -(可能需要严格相等运算符 !==), )

    • for C-like languages (eg. c#, javascript-(may need the strict equality operator !==), php)

      if (v != "x" && v != "y" && v != "z")
      {
          // the statements I want to be executed
          // if v is neither "x", nor "y", nor "z"
      }
    

  • 对于类似 Pascal 的语言

      IF (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-像语法)

    By De Morgan's law, the expression can also be rewritten as (using C-like syntax)

    !(v == "x" || v == "y" || v == "z")
    

    意义

    not((v 是x")or<代码>(v 是y")or(v 是z")).

    这使得逻辑更明显一些.

    This makes the logic a bit more obvious.

    某些语言具有用于测试集合成员资格的特定构造,或者您可以使用数组/列表操作.

    Some languages have specific constructs for testing membership in sets, or you can use array/list operations.

    • : v 不在 ('x', 'y', 'z')

    : [x",y",z"].indexOf(v) == -1

    : v 不在 {x"、y"、z"}

    :!Arrays.asList("x", "y", "z").contains(v)

    (及以上):!Set.of("x", "y", "z").contains(v)

    这篇关于为什么针对多个值对一个变量进行非等式检查总是返回 true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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