Groovy 中的决策制定

17 Mar 2025 | 阅读 2 分钟

在 groovy 中,决策制定用于检查条件并执行语句。如果条件为真,则执行 true 块语句;如果条件为假,则执行 false 块。

If 语句

在 groovy 中,if 语句用于只有一个条件的情况。在 if 语句中只有一个 true 块。如果条件为真,则执行 true 块。此语句中没有 false 或 else 块。

语法

流程图

Decision Making in Groovy

示例 1

输出

Decision Making in Groovy

示例 2

输出

Decision Making in Groovy

If else 语句

在 groovy 中,if else 语句用于只有一个条件的情况。在 if else 语句中,我们有一个 true 块和一个 false 块,或者我们可以说 else 块。如果条件为真,则执行 true 块,否则执行 else 块。

语法

流程图

Decision Making in Groovy

示例 3

输出

Decision Making in Groovy

示例 4

输出

Decision Making in Groovy

嵌套 If 语句

在 groovy 中,嵌套 if 语句用于有多个条件的情况。

语法

流程图

Decision Making in Groovy

示例 5

输出

Decision Making in Groovy

Switch 语句

在 groovy 中,switch 语句是一个多路分支语句。它提供了一种简单的方法来执行基于表达式值的代码的不同部分。

语法

流程图

Decision Making in Groovy

示例 6

输出

Decision Making in Groovy
下一主题Groovy - 字符串