当前位置:   article > 正文

Groovy~Groovy的条件语句_groovy if 多条件

groovy if 多条件

一、单分支语句

if (条件){执行语句}

class FirstTest{
    static void main(String[] args) {
        def a = 1
        if (a==1){
            println "单分支语句"
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

二、双分支语句

if (条件) {执行语句} else {执行语句}

class FirstTest{
    static void main(String[] args) {
        def a = 1
        if (a==0){
            println "a的值为0"
        } else {
            println "a的值不为0"
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

三、多分支语句

if (条件1) {执行语句1} else if (条件2) {执行语句2} else {执行语句3}

class FirstTest{
    static void main(String[] args) {
        def a = 1
        if (a == 0){
            println "a的值为0"
        } else if (a == 1){
            println "a的值为1"
        } else {
            println "a的值不是0也不是1"
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

switch(n){case n=a -> 执行语句a case n=b -> 执行语句b default -> 默认执行语句}

class FirstTest{
    static void main(String[] args) {
        def num = 3
        switch (num){
            case 1 ->
                println "num的值为1"
            case 2 ->
                println "null的值为2"
            default -> println "不知道num的值"
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/810355
推荐阅读
相关标签
  

闽ICP备14008679号