我想創建一個搜索函數,所以用戶可以通過使用鍵值進行搜索,但是試圖使用的方法返回類型不匹配,已經取出不必要的代碼並顯示什麼是需要。我如何設定積分來接受一個Int而不是「Any」?斯卡拉類型不匹配映射
「鍵入任何不符合類型爲int」
val mapdata = readFile("data.txt")
def handleTwo(): Boolean = {
mnuShowPointsForTeam(currentPointsForTeam)
true
}
def mnuShowPointsForTeam(f: (String) => (String, Int)) = {
print("Team>")
val data = f(readLine)
println(s"${data._1}: ${data._2}")
}
def currentPointsForTeam(team: String): (String, Int) = {
val points = mapdata.get(team) match{
case Some(p) => p
case None => 0
}
(team, points)
}
的data.txt中
SK1, 9, 7, 2, 0, 7, 3, 7, 9, 1, 2, 8, 1, 9, 6, 5, 3, 2, 2, 7, 2, 8, 5, 4, 5, 1, 6, 5, 2, 4, 1
SK2, 0, 7, 6, 3, 3, 3, 1, 6, 9, 2, 9, 7, 8, 7, 3, 6, 3, 5, 5, 2, 9, 7, 3, 4, 6, 3, 4, 3, 4, 1
SK4, 2, 9, 5, 7, 0, 8, 6, 6, 7, 9, 0, 1, 3, 1, 6, 0, 0, 1, 3, 8, 5, 4, 0, 9, 7, 1, 4, 5, 2, 8
SK5, 2, 6, 8, 0, 3, 5, 5, 2, 5, 9, 4, 5, 3, 5, 7, 8, 8, 2, 5, 9, 3, 8, 6, 7, 8, 7, 4, 1, 2, 3
SK6, 2, 7, 5, 9, 1, 9, 8, 4, 1, 7, 3, 7, 0, 8, 4, 5, 9, 2, 4, 4, 8, 7, 9, 2, 2, 7, 9, 1, 6, 9
SK7, 6, 9, 5, 0, 0, 0, 0, 5, 8, 3, 8, 7, 1, 9, 6, 1, 5, 3, 4, 7, 9, 5, 5, 9, 1, 4, 4, 0, 2, 0
SK8, 2, 8, 8, 3, 1, 1, 0, 8, 5, 9, 0, 3, 1, 6, 8, 7, 9, 6, 7, 7, 0, 9, 5, 2, 5, 0, 2, 1, 8, 6
SK9, 7, 1, 8, 8, 4, 4, 2, 2, 7, 4, 0, 6, 9, 5, 5, 4, 9, 1, 8, 6, 3, 4, 8, 2, 7, 9, 7, 2, 6, 6
mapData是什麼類型的? 「mnuShowPointsForTeam」的返回值是什麼(現在只是printlns)? – Thilo
更新了這個問題,mapdata是一個外部txt文件,是的,它只是printlns團隊多數民衆贊成密鑰值輸入@Thilo –
但是什麼是'mapData'類型?它是「Map [String,Any]」嗎?如果你不能創建'Map [String,Int]',那麼你必須在這個'case Some(p)'部分內部轉換/轉換爲Int。 – Thilo