在Groovy中如何做到这一点?

问题描述

| 可以说有两个变量,该变量的值取自用户。那是:
a=0
b=1
c=a-b
在某些情况下,我需要变量
c
始终为正,Groovy中是否有任何方法可以做到这一点?     

解决方法

有几种选择,取决于
c
为负时您实际想要的行为:
c = Math.abs(c) // -1 will become 1

c = Math.max(0,c) // -1 will become 0

// or it\'s an error condition
if( c < 0 ){
    tellUserToStopMessingAroundAndEnterTheCorrectValues()
    return
}