이항 연산자에 대한 잘못된 피연산자 유형, 정수로 작업하고 있다고 생각하지만 "<="가 작동하지 않음 [중복]

Dec 27 2020

부울을 사용할 수 없다는 오류가 계속 발생하고 정수가 필요하지만 N은 정수이므로 솔루션을 생각할 수 없습니다.

public static void main(String[] args) {
    int N = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    if(1<=N<=100){
        if(N % 2 != 0){
            System.out.println("Weird");
        } else {
            if(2<=N<=6){
                System.out.println("Not Weird");
            } else if (6<=N<=20){
                System.out.println("Weird");
            } else if(N>=20){
                System.out.println("Not Weird");
            }
        }
    }
    
    scanner.close();
}

답변

1 aran Dec 27 2020 at 15:21

Java에서는 직접 수행 할 수 없습니다.

if(1<=N<=100)

무슨 일이 발생 (1<=N)하는지 먼저 계산 하고 boolean.

그런 다음 ( [boolean]<=100) 컴파일을 시도하는데 , 이는 의미가 없습니다.

The operator <= is undefined for the argument type(s) boolean, int

귀하의 경우 :

The operator <= is undefined for the argument type(s) [1<=N]boolean, [100]int


다음 양식을 따르도록 모든 조건을 변경하십시오.

(min<=number && number<=max)

    int N = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    if(1<=N && N<=100)
    {
        if(N % 2 != 0)
            System.out.println("Weird");
        else 
        {
            if (2<=N && N<=6)
               System.out.println("Not Weird");
            else if (6<=N && N<=20)
               System.out.println("Weird");
            else if(N>=20)
               System.out.println("Not Weird");
         }
     }