score

70 阅读1分钟
object score01 {
  case class Stu(name: String, yuwen: Double, shuxue: Double, yingyu: Double)

  def main(args: Array[String]): Unit = {
    val stuList = scala.collection.mutable.ListBuffer[Stu]()

    val lines = scala.io.Source.fromFile("score.txt").getLines()

    lines.next()

    while (lines.hasNext) {
      val line = lines.next()
      val li = line.split("-")
      stuList += Stu(li(0), li(1).toDouble, li(2).toDouble, li(3).toDouble)
    }
    // 读入数据结束
    println("读入数据结束")

    stuList.foreach(stu => {
      val total = stu.yuwen + stu.yingyu + stu.shuxue
      val avg = total / 3
      println(s"${stu.name}, 总分: ${total} 平均分: ${avg}")
    })
  }
}