如何在 groovy 中使用整数除法运算符“/”?

Sky*_*ker 4 java groovy

我正在将一个 Java 项目迁移到 groovy。然后我得到一个if条件false在 Groovy 中给出,而true在 Java 代码中给出。

Groovy 示例代码:

int status_num = 301
if (status_num / 100 == 3) {
    throw new GoogleServiceConditionException("Google Search System is under maintenance")
}
Run Code Online (Sandbox Code Playgroud)

对于 groovy,if 条件为 false。对于 Java,它给出了 true。

我怎样才能缓解这个问题?

Sky*_*ker 10

我搜索了 Groovy 文档。它说如下:

对于 Java 中的整数除法,您应该使用intdiv()方法,因为 Groovy 不提供专用的整数除法运算符符号。

所以,我已经改变了如下代码。

if (status_num.intdiv(100) == 3) {
    throw new GoogleServiceConditionException("Google Search System is under maintenance")
}
Run Code Online (Sandbox Code Playgroud)

现在,它工作正常。

更多内容可以看教程:除法运算符的案例