Scala-不能递减for循环

Scala-Cannot decrement the for loop

  def start(digit:Int)
  {
    var max = "9"*digit.toInt
    var min = "1"+"0"*(digit-1).toInt

    for(i <- max to min by -1){
                 . . ^ (type mismatch; found : String required: scala.collection.generic.CanBuildFrom[Nothing,Char,?])


      var front = i
    }



  }

它说类型 "mismatch ;found:Stringrequired:scala.collection.generic.CanBuildFrom[Nothing,Char,?]"

也许你需要括号:

var max = ("9" * digit).toInt
var min = ("1"+"0"*(digit-1)).toInt

另一种选择是首先避免使用字符串 - 看起来使用 Math.pow 更直接地表示您要实现的目标:

val max = Math.pow(10, digit) - 1 // e.g. 9999.0 for digit = 4
val min = Math.pow(10, digit - 1) // e.g. 1000.0 for digit = 4