質問

確認方法を教えてください場合の条件パスを複数の値?

例:

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であなたは、単語を使用するか、およびC#では、

||使用します

私はPythonのソリューションを追加する何の言語を指定していないのでます:

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スタイルの言語を仮定します、ここで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);
    }

Javaいオブジェクトップリミティブ変数(整数int、Long、長します。だ値を比較すると多くの完成数(int)、そのままにしかできない開始っIntegerオブジェクト、もし内のlistなどの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 セット, きく異なる値のコレクションの並べ替え入力、プラス)なので、呼び出し .contains(Object) 方法の検索を進めていきます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top