import scala.io.Source
import scala.collection.mutable.ListBuffer
case class Student(name: String, yuwen: Double, shuxue: Double, yingyu: Double)
object StudentDataAssemble {
def main(args: Array[String]): Unit = {
val stuList = new ListBuffer[Student]()
val source = Source.fromFile("input.txt")
try {
for (line <- source.getLines()) {
if (line.nonEmpty) {
val fields = line.split(",")
if (fields.length == 4) {
val name = fields(0).trim
val yuwen = try { fields(1).trim.toDouble } catch { case _: Exception => 0.0 }
val shuxue = try { fields(2).trim.toDouble } catch { case _: Exception => 0.0 }
val yingyu = try { fields(3).trim.toDouble } catch { case _: Exception => 0.0 }
stuList += Student(name, yuwen, shuxue, yingyu)
} else {
println(s"数据格式错误,跳过此行:$line")
}
}
}
println("\n✅ 组装的学生对象列表:")
stuList.foreach(stu => println(s"姓名:${stu.name},语文:${stu.yuwen},数学:${stu.shuxue},英语:${stu.yingyu}"))
} catch { case ex: Exception => println(s"组装数据失败:${ex.getMessage}")
} finally {
source.close()
}
}
}