1.迭代器,跳过第一个元素 2.把字符串转成数字 3.如何判断一个正整数是否可以被三整除?(1+2+3)% 3==0
1.读入成绩,按行去读取-跳过第一个元素
val list=ListBuffer[Student]()
val it =Source.fromFile("score.txt").getLines().drop(1)
读入成绩-按行去读取
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
数学平均分
println("数学平均分",shuxuezongfen/list.length)
根据总分进行排序
val orderList = list.sortWith((a,b)=>a.total>b.total)
总代码
//1.迭代器,跳过第一个元素
//2.把字符串转成数字
//3.如何判断一个正整数是否可以被三整除?(1+2+3)% 3==0
import scala.collection.mutable.ListBuffer
import scala.io.Source
case class Student(name:String,yuwen:Int,shuxue:Int,total:Int,avg:Int)
object test {
def main(args: Array[String]): Unit = {
var shuxuezongfen=0
//1.读入成绩,按行去读取-跳过第一个元素
val list=ListBuffer[Student]()
val it =Source.fromFile("score.txt").getLines().drop(1)
//1.读入成绩-按行去读取
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
list +=Student(name, yuwen, shuxue, total, avg)
}
//数学平均分
println("数学平均分",shuxuezongfen/list.length)
//根据总分进行排序
val orderList = list.sortWith((a,b)=>a.total>b.total)
//打印结果
orderList.foreach(s=>println(s"姓名:${s.name},总分:${s.total}"))
//根据数学进行排序
list.sortWith((a,b)=>a.shuxue>b.shuxue)
.foreach(s=>println(s"姓名:${s.name},数学:${s.shuxue},总分:${s.total}"))
}
}
标签:arr,成绩,读取,迭代,toInt,list,shuxue,val,total From: https://blog.csdn.net/2301_82118291/article/details/144087477