for循环传入字符串的时候,提示 value foreach is not a member of String

wengcheng_k 2017-07-27

  在IDEA中运行就出现下面的错误:
def main(args: Array[String]) {
    var j: Long = 1
    for (i <- "Hello"){
      j =  j * i.toLong
    }
    println(j)
    println("-----------------------------")

  }

Error:(27, 15) value foreach is not a member of String
    for (i <- "Hello"){

在解释器中运行是可以的。
scala> var j: Long = 1
j: Long = 1

scala> for (i <- "Hello") j =  j * i.toLong

scala> println(j)
9415087488

scala>
Global site tag (gtag.js) - Google Analytics