【问题标题】:What is the return value of this conditional (groovy script)这个条件的返回值是什么(groovy脚本)
【发布时间】:2016-07-22 12:53:02
【问题描述】:

if/else 语句的返回值是多少?

if (salary <= 3000) {
  return discount < 0.40
}
else {
  return discount < 0.60
}

我不确定每个ifelse 的返回值是boolean 还是0.400.60

【问题讨论】:

    标签: if-statement groovy return-value


    【解决方案1】:

    这是一个布尔值。 &lt;&gt;!=&gt;===&lt;= 表达式将计算为布尔值:

    def mustDiscount(salary, discount = 0.5) {
        if (salary <= 3000) {
          return discount < 0.40
        }
        else {
          return discount < 0.60
        }
    }
    
    mustDiscount(2000).with {
        assert it in Boolean
        assert !it
    }
    
    
    mustDiscount(4000).with {
        assert it in Boolean
        assert it
    }
    

    【讨论】:

      【解决方案2】:

      if 块和 else 块中的返回类型都是布尔值(true 或 false)。

      return discount == 0.50 也会返回一个布尔值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-26
        • 2021-03-31
        • 1970-01-01
        • 2012-05-09
        • 2011-12-18
        • 1970-01-01
        • 2014-02-08
        • 2013-10-07
        相关资源
        最近更新 更多