package score
object score01 {
def main(args: Array[String]): Unit = {
val lines = scala.io.Source.fromFile("score.txt").getLines()
lines.next()
while(lines.hasNext){
val line = lines.next()
val li = line.split(",")
val name = li(0)
val yuwen = li(1)
println(s"${name},${yuwen}")
}
}
}
package score
import java.io.FileWriter
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.shuxue + stu.yingyu
val avg = total / 3
println(s"${stu.name},总分:${total},平均分:${avg}")
})
println("计算结束,开始写入")
val fileWriter = new FileWriter("score_result.txt")
fileWriter.write("高三19班成绩单 \n")
stuList.foreach(stu=>{
val total = stu.yuwen + stu.shuxue + stu.yingyu
val avg = total / 3
println(s"${stu.name},总分:${total},平均分:${avg} \n")
})
fileWriter.write(s"语文最高分:${stuList.map(_.yuwen).max} \n")
fileWriter.write(s"数学最高分:${stuList.map(_.shuxue).max} \n")
fileWriter.write(s"英语最高分:${stuList.map(_.yingyu).max} \n")
fileWriter.close()
}
}