简单一个!鉴于以下内容:
print(1 to 100 map {
case n if n % 10 == 0 => "Divisible by ten!"
case n if n % 5 == 0 => "Divisible by five!"
case n => n
})
制作它的最良心方式是什么,以便每次迭代后面都有换行符?我希望尽量减少必要的表达式块数。
答案 0 :(得分:7)
这是一种方式:
1 to 100 map {
case n if n % 10 == 0 => "Divisible by ten!"
case n if n % 5 == 0 => "Divisible by five!"
case n => n
} foreach println _
或者
println(
1 to 100 map {
case n if n % 10 == 0 => "Divisible by ten!"
case n if n % 5 == 0 => "Divisible by five!"
case n => n
} mkString "\n"
)