100 lt;= x lt;= 150 as argument in if (), acting funny(100 lt;= x lt;= 150 作为 if () 中的参数,表现得很有趣)
问题描述
我有一个 if
语句,后跟几个 else if
语句.所有 if/else if 语句都有一个结构如下的参数:
I have an if
statement followed by several else if
statements. All of the if/else if statements have an argument structured something like this:
if (100 <= x <= 149) //do this
else if (150 <= x <= 199) //do that
else if ...etc...
但是,由于某种原因,只有第一个 if 语句才会被执行.X 可以是 200,并且只会识别第一个 if 语句.
However, for some reason only the first if statement ever gets executed. X can be 200 and only the first if statement will be recognized.
我不知道为什么当 X 不符合前一个语句的参数时,它没有转到下一个 else if 语句.这在 Obj-C 中不起作用吗?任何帮助表示赞赏.谢谢
I'm not sure why it isn't moving on to the next else if statement when X doesn't fit the argument of the preceding statement. Does this not work in Obj-C? Any help is appreciated. Thanks
推荐答案
你需要改写如下语句:
if (x >= 100 && x <= 149) {
} else if (x >= 150 && x <= 199) {
} ...
你的第一个 if
被评估为:
Your first if
is evaluated like:
if ((100 <= x) <= 149)
让我们看看它是如何评估的:
Let's have a look how that evaluates:
- 如果
x = 200
,则(100 <= 200)
为真,因此计算结果为值1
(表示真).然后1 <= 149
也是如此. - 如果
x
的值小于 100,例如10
,则(100 <= 10)
为假,因此计算到值0
(这意味着 false).同样,0 <= 149
为真.
- If
x = 200
, then(100 <= 200)
is true and thus evaluates to the value1
(which means true). And then1 <= 149
is also true. - If
x
has a value smaller than 100, for example10
, then(100 <= 10)
is false and thus evaluates to the value0
(which means false). Again,0 <= 149
is true.
所以不管 x
的值如何,整个表达式总是为真.
So regardless of the value of x
, the whole expression will always be true.
这篇关于100 <= x <= 150 作为 if () 中的参数,表现得很有趣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:100 <= x <= 150 作为 if () 中的参数,表现得很有趣


- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01