package com.test_32
//1,迭代器,跳过第一个元素
import scala.collection.mutable.ListBuffer
import scala.io.Source
//成绩分析
case class Student(name:String,yumen:Int,shuxue:Int,yinyu:Int,total:Int,avg:Int)
object test1 {
def main(args: Array[String]): Unit = {
//读入成绩-按行去读取-跳过第一个元素
val list=ListBuffer[Student]()
val it = Source.fromFile("score.txt").getLines().drop(1)
var shuxuezongfen=0
while (it.hasNext) {
val content = it.next()
//使用中文的逗号去拆分字符串
var arr = content.split(",")
val name=arr(0)
val yuwen=arr(1).toInt
val shuxue=arr(2).toInt
val yingyu=arr(3).toInt
val total=yuwen+shuxue+yingyu
val avg=total/3
shuxuezongfen+=shuxue
//println("当前行是", name,yingyu,total,avg)
list+=Student(name,yuwen,shuxue,yingyu,total,avg)
}
//数学平均分
println("数学平均分",shuxuezongfen/list.length)
//根据总分进行排序
val orderList=list.sortWith((a,b)=>a.total>b.total).slice(0,3)
//打印结果
orderList.foreach(s=>println(s"姓名:${s.name},数学:${s.name},总分:${s.total}"))
orderList.foreach(s=>println(s"姓名:${s.name},总分:${s.total}"))
}
}
标签:同学,arr,name,val,Int,shuxue,总分,total,平均分
From: https://blog.csdn.net/zxfgdjfjfjflfllf/article/details/144087479