开发者

How to do this in Groovy?

开发者 https://www.devze.com 2023-02-25 05:01 出处:网络
Lets say that there are two variable, the value of the variable is taken from the users. That is: a=0 b=1

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

精彩评论

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