1

条件が複数の値を渡すかどうかを確認するにはどうすればよいですか?

例:

if(number == 1,2,3)

コンマが機能しないことはわかっています。

4

9 に答える 9

3
if (number == 1 || number == 2 || number == 3)
于 2009-10-07T14:38:47.790 に答える
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 ] )

それが役立つことを願っています。

于 2011-06-13T17:46:11.137 に答える
1
if ((number >= 1) && (number <= 3))
于 2009-10-07T14:37:55.870 に答える
1

何語?

たとえば、VB.NET では OR という単語を使用し、C# では || を使用します。

于 2009-10-07T14:38:30.223 に答える
1

言語を指定しないので、Python ソリューションを追加します。

if number in [1, 2, 3]:
    pass
于 2009-10-07T14:41:11.420 に答える
0

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))
于 2009-10-07T14:40:33.950 に答える
0

ここでは、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)
}

これらのチェックを連鎖させて、かなり複雑なロジックを作成する方法がわかります。

于 2009-10-07T14:46:26.417 に答える
0

Java では、プリミティブ変数 (int の場合は Integer、long の場合は Long など) をラップするオブジェクトがあります。多数の完全な数値 (int) の間で値を比較する場合、できることは、一連の 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
    }
}

もちろん、いくつかの値の場合、これは少し扱いに​​くいかもしれませんが、多くの値と比較したい場合はうまく機能します。

もう 1 つのオプションは、Java Setsを使用することです。さまざまな値を配置し (コレクションによって入力が並べ替えられますが、これはプラスです)、.contains(Object)メソッドを呼び出して等価性を見つけることができます。

于 2015-08-04T08:55:16.487 に答える