我正在尝试创建搜索功能,因此用户可以使用键值搜索列表,但是我尝试使用的方法返回类型不匹配,已经取出了不必要的代码并显示了所需的内容。如何设置点以获取Int而不是“Any”?
“类型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
答案 0 :(得分:3)
看起来您想要返回一个List[Int]
的元组,而不仅仅是一个Int
。
如果是的话
def currentPointsForTeam(team: String): (String, List[Int]) =
(team, mapdata.get(team).getOrElse(List.empty))
// Or maybe List(0) instead of List.empty
如果您确实要返回单个Int
,则必须说明如何从地图中的List[Int]
转到单个值。也许一笔钱?
def currentPointsForTeam(team: String): (String, Int) =
(team, mapdata.get(team).map(_.sum).getOrElse(0))