假设有两个变量,变量的值取自用户。即:
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
}
关于groovy - 如何在 Groovy 中做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694427/