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 <= 149is also true. - If
xhas a value smaller than 100, for example10, then(100 <= 10)is false and thus evaluates to the value0(which means false). Again,0 <= 149is true.
所以不管 x 的值如何,整个表达式总是为真.
So regardless of the value of x, the whole expression will always be true.
这篇关于100 <= x <= 150 作为 if () 中的参数,表现得很有趣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:100 <= x <= 150 作为 if () 中的参数,表现得很有趣
基础教程推荐
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
