Gin*_*as_ 0 c# if-statement numbers relational operators
我正在检查0和-0.2之间的值是否如此:
float value = -0.1;
if (0 < value > -0.2f) { }
Run Code Online (Sandbox Code Playgroud)
但我得到错误operator '>' cannot be applied to operands of type 'bool' and 'float'.我知道,我可以通过其他方式使用&&运算符,如果句子,但我想知道,为什么它被检测为bool?
看看这个:
0 < value > -0.2f
Run Code Online (Sandbox Code Playgroud)
首先0 < value进行评估,它返回bool值然后尝试将它与bool浮点数进行比较-0.2f,因此它返回错误,因为它是不允许的.
您想检查值是否更小以及值是否大于,因此这是您要执行的操作:
if (0 < value && value > -0.2f) { }
Run Code Online (Sandbox Code Playgroud)