【问题标题】:Objective-C, C multiple equality signs in one if [duplicate]Objective-C,C多个等号在一个if [重复]
【发布时间】:2014-09-01 08:58:18
【问题描述】:

我确实写了一份声明

if (sample.value > shortAverage > shortWithoutFirstAverage > longAverage) {

并且期望 sample.value > longAverage

但这不起作用,并且可能会发生 sample.value

(lldb) p (sample.value > shortAverage > shortWithoutFirstAverage > longAverage)
(bool) $13 = true
(lldb) p sample.value
(double) $14 = 0.029
(lldb) p longAverage
(double) $15 = 0.032
(lldb) p (sample.value > longAverage)
(bool) $16 = false
(lldb) p shortAverage

其他值

(double) $17 = 0.029821
(lldb) p shortWithoutFirstAverage
(double) $18 = 0.029802  

看起来它被评估为

if ((sample.value > shortAverage) > (shortWithoutFirstAverage > longAverage)) {

但是如何实现所需的 if 语句呢?这样的话,(sample.value > shortAverage > shortWithoutFirstAverage > longAverage)

【问题讨论】:

    标签: objective-c c


    【解决方案1】:

    你不能检查这样的表达式-

    if (sample.value > shortAverage > shortWithoutFirstAverage > longAverage) 
    

    使用-

    if ( (shortWithoutFirstAverage > longAverage) && (shortAverage > shortWithoutFirstAverage) && (sample.value > shortAverage) )
    

    【讨论】:

      【解决方案2】:

      您不能像这样在 C 和相关语言中链接二元运算符,例如 >

      变化:

      if (sample.value > shortAverage > shortWithoutFirstAverage > longAverage) {
      

      到:

      if (sample.value > shortAverage &&
          shortAverage > shortWithoutFirstAverage &&
          shortWithoutFirstAverage > longAverage) {
      

      【讨论】:

        【解决方案3】:

        就这样吧:

        if ((sample.value > shortAverage) && (shortAverage > shortWithoutFirstAverage) && (shortWithoutFirstAverage > longAverage))

        【讨论】:

          猜你喜欢
          • 2017-01-03
          • 2014-09-03
          • 1970-01-01
          • 1970-01-01
          • 2015-03-01
          • 2019-08-26
          • 1970-01-01
          • 2013-12-12
          • 1970-01-01
          相关资源
          最近更新 更多