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 ){
    tellUserToStopMessingAroundAndEnterTheCorrectValues()
    return
}

关于groovy - 如何在 Groovy 中做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694427/

相关文章:

scripting - 在应用程序中使用 Groovy 脚本的最佳方法

java - Gson序列化字段仅当不为空或不为空时

grails - 时髦的错误? -- 创建一个初始化的 float 组?

grails - 对象未保存在Spock集成测试中

grails - 在Groovy Controller /服务中使用非默认dataSource

ant - Ant : Use script task or groovy task? 中的 Groovy 脚本

grails - 在Grails中跨多个月的集合中按月选择项目

android - Gradle任务开始时不调用

gradle - Gradle Zip Task永久运行取决于目标目录

java - 正则表达式模式帮助 java/groovy