Groovy 代码报错:字符串与数字相乘错误
这段代码报错的原因是在计算 'ss' 的值时,将字符串 'nn' 与字符串 'src' 相乘。在 Groovy 中,不能直接将字符串与数字相乘,需要先将字符串转换为数字再进行计算。
可以使用 'toInteger()' 方法将字符串转换为整数,然后再进行乘法运算。修改代码如下:
String ret = 'kl'
String src = '20'
String temp = '20L/桶'
String param0 = ''
def nn = ''
def ss = ''
temp = temp.replace('L','')
def bmbmList = temp.split('/')
nn = bmbmList[0].toInteger()
if(bmbmList[1] != ret)
{
ss = nn * src.toInteger()
}
println ss
这样就能正确地计算出 'ss' 的值了。
原文地址: https://www.cveoy.top/t/topic/qnV1 著作权归作者所有。请勿转载和采集!