معرفة ما اذا كان متغير لديه أي قيمة من مجموعة معينة

StackOverflow https://stackoverflow.com/questions/1532036

سؤال

وكيف يمكنني معرفة ما إذا كان الشرط يمر قيم متعددة؟

مثال:

if(number == 1,2,3)

وأنا أعلم أن الفواصل لا عمل.

هل كانت مفيدة؟

المحلول

if (number == 1 || number == 2 || number == 3)

نصائح أخرى

إذا كنت تستخدم 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 ((number >= 1) && (number <= 3))

ما هي اللغة؟

وعلى سبيل المثال في VB.NET استخدام كلمة OR، وفي C # استخدام ||

ومنذ تحديد أي لغة أود أن أضيف إلى حل بيثون:

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

في T-SQL يمكنك استخدام عامل التشغيل IN:

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

إذا كنت تستخدم مجموعة قد يكون هناك يحتوي على مشغل عن طريقة أخرى للقيام بذلك.

في C # عن طريقة أخرى قد يكون من الأسهل لإضافة القيم:

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

وسوف أفترض لغة C-STYLE، وهنا التمهيدي سريعة على 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)
}

وهكذا يمكنك أن ترى كيف يمكنك سلسلة لهذه الشيكات معا لخلق بعض المنطق معقدة جدا.

لقائمة من الأعداد الصحيحة:

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);
    }

في جاوة لديك الكائنات التي التفاف المتغيرات البدائية (عدد صحيح لكثافة العمليات، لونغ لفترة طويلة، الخ). إذا كنت تبحث لمقارنة القيم بين الكثير من الأرقام كاملة ([إينتس])، ما يمكنك القيام به هو الشروع مجموعة من الكائنات صحيح، الاشياء لهم داخل مثل iterable باعتبارها 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
    }
}

وبطبيعة الحال لبضع قيم هذا قد يكون غير عملي بعض الشيء، ولكن إذا كنت ترغب في مقارنة ضد الكثير من القيم، وأنها سوف تعمل بشكل جيد.

وثمة خيار آخر هو استخدام جافا مجموعات ، يمكنك وضع الكثير من القيم المختلفة (وجمع فرز المدخلات الخاصة بك، والذي هو زائد) ومن ثم استدعاء الأسلوب .contains(Object) لتحديد المساواة.

scroll top