现在有个价格的集合。如果价格大于20,先九折,然后相加。如果用scala实现
方式 一、main方法作为程序的入口
object Prices {
def main(args:Array[String]):Unit = {
val prices = Array(10,30,17,20,15,18,45,12)
val total = prices.filter(x => x >20).map(x=>x*0.9).reduce((x,y) => x+y)
println("total is :" +total)
}
}
方式二、通过extends App定义
object Prices extends App{
val prices = Array(10,30,17,20,15,18,45,12)
val total = prices.filter(x => x >20).map(x=>x*0.9).reduce((x,y) => x+y)
println("total is :" +total)
}
二者执行结果均为total is :67.5