如何在Groovy中实现这一点?

如何在Groovy中实现这一点?,groovy,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 ){ tellUserToStopMessingAroundA

假设有两个变量,变量的值取自用户。即:

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
}
c=Math.abs(c)/-1将变为1
c=数学。最大值(0,c)/-1将变为0
//或者这是一个错误条件
if(c<0){
告诉用户ToStopMessingRound并输入正确的值()
返回
}

1)您希望如何确保该值为正值?2) 这是Groovy特有的吗?3) 是否希望通知用户无法输入某个值?如果不是,您可以使用
If(c<0)
,其他所有编程语言都是一样的。我得到了我想要的答案!我只希望
c
为正(即使计算结果为负)