开发者

How to do this in Groovy?

Lets say that there are two variable, the value of the variable is taken from the users. That is:

a=0
b=1
c=a-b

F开发者_JAVA技巧or some cases i need the variable c to be always positive, is there is any method in Groovy to do this?


Couple of options, depending on want behaviour you actually want when c is negative:

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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜