scala编写独立的应用程序,找出最高评分的店铺和最低分的店铺
时间: 2023-05-25 22:01:23 浏览: 65
A:以下是Scala代码实现:
```scala
import scala.io.Source
object Main extends App {
case class Store(name: String, rating: Double)
// 读取文件中的店铺数据
val stores = Source.fromFile("stores.txt").getLines().map { line =>
val Array(name, ratingStr) = line.split(",")
Store(name, ratingStr.toDouble)
}.toList
// 找出最高评分的店铺
val highestRatingStore = stores.maxBy(_.rating)
println(s"Highest rating store: ${highestRatingStore.name}, rating: ${highestRatingStore.rating}")
// 找出最低分的店铺
val lowestRatingStore = stores.minBy(_.rating)
println(s"Lowest rating store: ${lowestRatingStore.name}, rating: ${lowestRatingStore.rating}")
}
```
注:需要在代码所在的目录下放置名为`stores.txt`的文件,文件中每行为一条店铺数据,格式为`name, rating`。例如:
```
Store A, 4.5
Store B, 3.2
Store C, 2.7
Store D, 4.8
```