如果你想建立一个
String
具有的值
n
零,您可以使用
for
让角色屈服
0
然后返回
Vector
到字符串,使用
mkString
具体如下:
scala> val aLotOfZeroes: String = (for (i <- 0 to 63) yield "0").mkString
aLotOfZeroes: String = 0000000000000000000000000000000000000000000000000000000000000000
然后,您可以通过添加类似的参数来概括它:
scala> def aLotOfZeroes(n: Int): String = (for (i <- 0 to n) yield "0").mkString
aLotOfZeroes: (n: Int)String
scala> aLotOfZeroes(10)
res2: String = 00000000000
scala> val zeroes: String = aLotOfZeroes(10)
zeroes: String = 00000000000
scala> zeroes
res3: String = 00000000000
此外,从@dividebyzero的注释中,您可以使用
*
:
scala> "0" * 64
res13: String = 0000000000000000000000000000000000000000000000000000000000000000
并定义:
scala> def aLotOfZeroes: Int => String = "0" * _
aLotOfZeroes: Int => String
scala> aLotOfZeroes(10)
res16: String = 0000000000