检查变量是否具有给定集合中的任何值

发布于 2024-08-06 18:52:28 字数 100 浏览 4 评论 0原文

如何检查一个条件是否传递多个值?

示例:

if(number == 1,2,3)

我知道逗号不起作用。

How can I check if a condition passes multiple values?

Example:

if(number == 1,2,3)

I know that commas don't work.

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

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

发布评论

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

评论(9

明月夜 2024-08-13 18:52:28
if (number == 1 || number == 2 || number == 3)
if (number == 1 || number == 2 || number == 3)
颜漓半夏 2024-08-13 18:52:28

如果您使用 PHP,那么假设您的数字列表是一个数组,

$list = array(1,3,5,7,9);

那么对于任何元素,您可以使用

if(in_array($element, $list)){
//Element present in list
}else{
//not present.
}

函数结构:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

希望有帮助。

If you are using PHP, then suppose your list of numbers is an array

$list = array(1,3,5,7,9);

then for any element, you can use

if(in_array($element, $list)){
//Element present in list
}else{
//not present.
}

Function structure:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Hope that helps.

活雷疯 2024-08-13 18:52:28
if ((number >= 1) && (number <= 3))
if ((number >= 1) && (number <= 3))
尴尬癌患者 2024-08-13 18:52:28

什么语言?

例如,在 VB.NET 中使用 OR,在 C# 中使用 ||

What language?

For example in VB.NET you use the word OR, and in C# you use ||

喜你已久 2024-08-13 18:52:28

由于您没有指定语言,我添加了一个 Python 解决方案:

if number in [1, 2, 3]:
    pass

Since you specify no language I add a Python solution:

if number in [1, 2, 3]:
    pass
人心善变 2024-08-13 18:52:28

在 T-SQL 中,您可以使用 IN 运算符:

select * from MyTable where ID in (1,2,3)

如果您使用的是集合,则可能有一个 contains 运算符,可以通过另一种方式来执行此操作。

在 C# 中,另一种可能更容易添加值的方法:

    List<int> numbers = new List<int>(){1,2,3};
    if (numbers.Contains(number))

In T-SQL you can use the IN operator:

select * from MyTable where ID in (1,2,3)

If you are using a collection there may be a contains operator for another way to do this.

In C# for another way that may be easier to add values:

    List<int> numbers = new List<int>(){1,2,3};
    if (numbers.Contains(number))
窝囊感情。 2024-08-13 18:52:28

我将假设使用 C 风格语言,这里是 IF AND OR 逻辑的快速入门:

if(variable == value){
    //does something if variable is equal to value
}

if(!variable == value){
    //does something if variable is NOT equal to value
}

if(variable1 == value1 && variable2 == value2){
    //does something if variable1 is equal to value1 AND variable2 is equal to value2
}

if(variable1 == value1 || variable2 = value2){
    //does something if variable1 is equal to value1 OR  variable2 is equal to value2
}

if((variable1 == value1 && variable2 = value2) || variable3 == value3){
    //does something if:
    // variable1 is equal to value1 AND variable2 is equal to value2
    // OR variable3 equals value3 (regardless of variable1 and variable2 values)
}

if(!(variable1 == value1 && variable2 = value2) || variable3 == value3){
    //does something if:
    // variable1 is NOT equal to value1 AND variable2 is NOT equal to value2
    // OR variable3 equals value3 (regardless of variable1 and variable2 values)
}

因此您可以看到如何将这些检查链接在一起以创建一些相当复杂的逻辑。

I'll assume a C-Style language, here's a quick primer on IF AND OR logic:

if(variable == value){
    //does something if variable is equal to value
}

if(!variable == value){
    //does something if variable is NOT equal to value
}

if(variable1 == value1 && variable2 == value2){
    //does something if variable1 is equal to value1 AND variable2 is equal to value2
}

if(variable1 == value1 || variable2 = value2){
    //does something if variable1 is equal to value1 OR  variable2 is equal to value2
}

if((variable1 == value1 && variable2 = value2) || variable3 == value3){
    //does something if:
    // variable1 is equal to value1 AND variable2 is equal to value2
    // OR variable3 equals value3 (regardless of variable1 and variable2 values)
}

if(!(variable1 == value1 && variable2 = value2) || variable3 == value3){
    //does something if:
    // variable1 is NOT equal to value1 AND variable2 is NOT equal to value2
    // OR variable3 equals value3 (regardless of variable1 and variable2 values)
}

So you can see how you can chain these checks together to create some pretty complex logic.

窝囊感情。 2024-08-13 18:52:28

对于整数列表:

static bool Found(List<int> arr, int val)
    {
        int result = default(int);
        if (result == val)
            result++;

        result = arr.FindIndex(delegate(int myVal)
        {
            return (myVal == val);
        });
        return (result > -1);
    }

For a list of integers:

static bool Found(List<int> arr, int val)
    {
        int result = default(int);
        if (result == val)
            result++;

        result = arr.FindIndex(delegate(int myVal)
        {
            return (myVal == val);
        });
        return (result > -1);
    }
无畏 2024-08-13 18:52:28

在 Java 中,有包装原始变量的对象(Integer 表示 int,Long 表示 long 等)。如果您希望比较大量完整数字(整数)之间的值,您可以做的是启动一堆 Integer 对象,将它们填充到可迭代对象(例如 ArrayList)中,迭代它们并进行比较。

类似于:

ArrayList<Integer> integers = new ArrayList<>();
integers.add(13);
integers.add(14);
integers.add(15);
integers.add(16);

int compareTo = 17;
boolean flag = false;
for (Integer in: integers) {
    if (compareTo==in) {
    // do stuff
    }
}

当然,对于一些值来说,这可能有点笨拙,但如果您想与很多值进行比较,它会很好地工作。

另一种选择是使用 java Sets,您可以放置很多不同的值(集合会对您的输入进行排序,这是一个优点),然后调用 .contains(Object) 方法来定位相等性。

In Java you have objects that wrap primitive variables (Integer for int, Long for long, etc). if you look to compare values between a lot of complete numbers (ints), what you can do is initiate a bunch of Integer objects, stuff them inside an iterable such as an ArrayList, iterate over them and compare.

something like:

ArrayList<Integer> integers = new ArrayList<>();
integers.add(13);
integers.add(14);
integers.add(15);
integers.add(16);

int compareTo = 17;
boolean flag = false;
for (Integer in: integers) {
    if (compareTo==in) {
    // do stuff
    }
}

of course for a few values this may be a bit unwieldy, but if you want to compare against a lot of values, it'll work nicely.

Another option is to use java Sets, you can place a lot of different values (the collection will sort your input, which is a plus) and then invoke the .contains(Object) method to locate equality.

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