문제

조건이 여러 값을 전달하는지 어떻게 확인할 수 있습니까?

예시:

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#에서 사용 ||

언어를 지정하지 않기 때문에 파이썬 솔루션을 추가합니다.

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 orr 또는 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)
}

따라서 이러한 점검을 함께 체인하여 꽤 복잡한 논리를 만들 수있는 방법을 알 수 있습니다.

정수 목록의 경우 :

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의 정수, 긴 장기 등). 많은 완전한 숫자 (ints) 사이의 값을 비교하려면, 당신이 할 수있는 일은 정수 개체를 시작하고, 배열 목록과 같은 반복 가능한 반복 안에 넣고 반복하고 비교할 수 있습니다.

같은 것 :

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