我只是想获取与 String 键关联的 Int 值。我得到:
error: type mismatch;
found : Char
required: String
score += tiles(letter)
从我这里的代码:
val tiles = Map[String, Int](
"a" -> 1,
"b" -> 3,
"c" -> 3,
"d" -> 2
// etc.
)
def main(args: Array[String]) {
println("\nScrabble Calculator 1.0")
println("Enter words on the commandline.")
println("Use a '_' character for blank tiles.\n")
for (w <- args) // loop through each word
if (w.length < 2)
println(w + ": one-letter words disallowed in Scrabble")
else
calculate(w)
}
def calculate(w: String) {
var score = 0
for (letter <- w)
score += tiles(letter)
println(w + ": " + score + " points")
}
如果我使用 "a" 或 "b" 而不是letter
,它可以正常工作(它返回 1 或 3 或其他)。