我怎样才能添加下一个列表来做一个总和和平均?
How can I add the next list to do a sum and avarage?
我得到了以下代码:
def arrayOfInts = [actual_speed_mobile_1.toInteger(), actual_speed_mobile_2.toInteger(), actual_speed_mobile_3.toInteger(), actual_speed_mobile_4.toInteger(), actual_speed_mobile_5.toInteger()]
"this is **important** if we use removeAt"
Collections.sort(arrayOfInts)
def min = Collections.min(arrayOfInts);
WebUI.comment("our min value is " + min.toString())
def max = Collections.max(arrayOfInts);
WebUI.comment("our max value is " + max.toString())
WebUI.comment("our list of values are " + arrayOfInts.toString())
arrayOfInts.removeAt(0);
WebUI.comment("our list of values are " + arrayOfInts.toString())
if (min != max) {
arrayOfInts.removeAt(arrayOfInts.size() - 1);
WebUI.comment("our list of values are " + arrayOfInts.toString())
}
WebUI.comment("our values are " + arrayOfInts.toString())
输出为:
arrayOfInts = [actual_speed_mobile_1.toInteger(), actual_speed_mobile_2.toInteger(), actual_speed_mobile_3.toInteger(), actual_speed_mobile_4.toInteger(), actual_speed_mobile_5.toInteger()]
Collections.sort(arrayOfInts)
in = Collections.min(arrayOfInts)
comment("our min value is " + min.toString())
our min value is 74
max = Collections.max(arrayOfInts)
comment("our max value is " + max.toString())
our max value is 86
comment("our list of values are " + arrayOfInts.toString())
our list of values are [74, 82, 83, 84, 86]
arrayOfInts.removeAt(0)
comment("our list of values are " + arrayOfInts.toString())
our list of values are [82, 83, 84, 86]
if (min != max)
arrayOfInts.removeAt(arrayOfInts.size() - 1)
comment("our list of values are " + arrayOfInts.toString())
our list of values are [82, 83, 84]
我希望列表做平均 我想将它添加到一个新变量并将其转换为 inter 但我得到了错误:
Reason:
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.toInterger() is applicable for argument types: () values: []
at Aspire_cashout.run(Aspire_cashout:115)
我补充一下:
for(int i=0; i<arrayOfInts.size(); i++)
total = total + arrayOfInts[i];
}
WebUI.comment("our values are " + arrayOfInts.toString())
def avarage = total/arrayOfInts.size();
println(avarage.toString())
也许我可以帮助某人
您的代码可以用更惯用的方式重写 groovy 例如:
//def arrayOfInts = [actual_speed_mobile_1.toInteger(), actual_speed_mobile_2.toInteger(), actual_speed_mobile_3.toInteger(), actual_speed_mobile_4.toInteger(), actual_speed_mobile_5.toInteger()]
def strings = ["1", "2", "3", "4", "5"]
def ints = strings.collect { it as Integer}.sort()
println "ints: $ints"
ints.removeAt(0)
println "ints: $ints"
if (ints.min() != ints.max()) {
// init returns a list of all elements except the last
ints = ints.init()
}
println "ints: $ints"
println "average: ${ints.average()}"
def avg = ints.sum() / ints.size()
println "average: ${avg} (groovy < 3.x)"
其中 iterable.average()
方法是在 groovy 3.0 中引入的。如果您 运行 的版本比 groovy 早,则可以使用 ints.sum() / ints.size()
结构。
执行时(使用 groovy 3.x)上面的代码导致:
─➤ groovy -v
Groovy Version: 3.0.6 JVM: 11.0.10 Vendor: Amazon.com Inc. OS: Linux
─➤ groovy solution.groovy
ints: [1, 2, 3, 4, 5]
ints: [2, 3, 4, 5]
ints: [2, 3, 4]
average: 3
average: 3 (groovy < 3.x)
─➤
我得到了以下代码:
def arrayOfInts = [actual_speed_mobile_1.toInteger(), actual_speed_mobile_2.toInteger(), actual_speed_mobile_3.toInteger(), actual_speed_mobile_4.toInteger(), actual_speed_mobile_5.toInteger()]
"this is **important** if we use removeAt"
Collections.sort(arrayOfInts)
def min = Collections.min(arrayOfInts);
WebUI.comment("our min value is " + min.toString())
def max = Collections.max(arrayOfInts);
WebUI.comment("our max value is " + max.toString())
WebUI.comment("our list of values are " + arrayOfInts.toString())
arrayOfInts.removeAt(0);
WebUI.comment("our list of values are " + arrayOfInts.toString())
if (min != max) {
arrayOfInts.removeAt(arrayOfInts.size() - 1);
WebUI.comment("our list of values are " + arrayOfInts.toString())
}
WebUI.comment("our values are " + arrayOfInts.toString())
输出为:
arrayOfInts = [actual_speed_mobile_1.toInteger(), actual_speed_mobile_2.toInteger(), actual_speed_mobile_3.toInteger(), actual_speed_mobile_4.toInteger(), actual_speed_mobile_5.toInteger()]
Collections.sort(arrayOfInts)
in = Collections.min(arrayOfInts)
comment("our min value is " + min.toString())
our min value is 74
max = Collections.max(arrayOfInts)
comment("our max value is " + max.toString())
our max value is 86
comment("our list of values are " + arrayOfInts.toString())
our list of values are [74, 82, 83, 84, 86]
arrayOfInts.removeAt(0)
comment("our list of values are " + arrayOfInts.toString())
our list of values are [82, 83, 84, 86]
if (min != max)
arrayOfInts.removeAt(arrayOfInts.size() - 1)
comment("our list of values are " + arrayOfInts.toString())
our list of values are [82, 83, 84]
我希望列表做平均 我想将它添加到一个新变量并将其转换为 inter 但我得到了错误:
Reason:
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.toInterger() is applicable for argument types: () values: []
at Aspire_cashout.run(Aspire_cashout:115)
我补充一下:
for(int i=0; i<arrayOfInts.size(); i++)
total = total + arrayOfInts[i];
}
WebUI.comment("our values are " + arrayOfInts.toString())
def avarage = total/arrayOfInts.size();
println(avarage.toString())
也许我可以帮助某人
您的代码可以用更惯用的方式重写 groovy 例如:
//def arrayOfInts = [actual_speed_mobile_1.toInteger(), actual_speed_mobile_2.toInteger(), actual_speed_mobile_3.toInteger(), actual_speed_mobile_4.toInteger(), actual_speed_mobile_5.toInteger()]
def strings = ["1", "2", "3", "4", "5"]
def ints = strings.collect { it as Integer}.sort()
println "ints: $ints"
ints.removeAt(0)
println "ints: $ints"
if (ints.min() != ints.max()) {
// init returns a list of all elements except the last
ints = ints.init()
}
println "ints: $ints"
println "average: ${ints.average()}"
def avg = ints.sum() / ints.size()
println "average: ${avg} (groovy < 3.x)"
其中 iterable.average()
方法是在 groovy 3.0 中引入的。如果您 运行 的版本比 groovy 早,则可以使用 ints.sum() / ints.size()
结构。
执行时(使用 groovy 3.x)上面的代码导致:
─➤ groovy -v
Groovy Version: 3.0.6 JVM: 11.0.10 Vendor: Amazon.com Inc. OS: Linux
─➤ groovy solution.groovy
ints: [1, 2, 3, 4, 5]
ints: [2, 3, 4, 5]
ints: [2, 3, 4]
average: 3
average: 3 (groovy < 3.x)
─➤