如何使用整数除法运算符" /"在groovy?

时间:2018-05-30 15:33:01

标签: java groovy

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

Groovy示例代码:

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

if条件为groovy提供了错误。对于Java,它给出了真实的。

如何缓解此问题?

1 个答案:

答案 0 :(得分:3)

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

  

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

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

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

现在,它运作正常。

有关更多信息,请参阅教程: The case of the division operator

相关问题