简体   繁体   中英

basic if statement, operator <= undefined

I'm new to programming

if( (N%2==0) && (6<=N<=20) ) 

Throws the error below

The operator <= is undefined for the argument type(s) boolean , int
Please help me fix it.

You can't compound the statement like that. You need to && it.

For example,

if ((N % 2 == 0) && (6 <= N && N <= 20)) {...} 

The reason you get the error is the first condition of 6 <= N resolves to a boolean and you then attempt to check if a boolean is <= to an int . That does not compute.

您无法在一次检查中比较2个条件,您需要将其拆分为两个检查

if (N % 2 == 0 && N >= 6 && N <= 20) 

您应该使用逻辑运算符(在这种情况下为&&)分隔条件:

if (N % 2 == 0 && N>=6 && N <= 20)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM