scala 成绩分析

ort scala.io.Source
 
object Test文件读写_成绩分析 {
  def main(args: Array[String]): Unit = {
    //1.按行读入
    val source = Source.fromFile("score.txt")
    val it = source.getLines()
    it.next()//跳过第一行
    while (it.hasNext){
      println(it.next())
    }
    source.close()
  }
}
2.处理数据:

        (1)计算每个同学的总分和平均分

import scala.collection.mutable.ListBuffer
import scala.io.Source
case class Student(name: String, yuwen: Double, shuxue: Double, yingyu: Double, total: Double, avg: Double)
//案例分析:统计成绩
object Test文件读写_成绩分析 {
  def main(args: Array[String]): Unit = {
 
    //可变List,保存索引的学生数据
    val studentList = ListBuffer[Student]()
 
    //1.按行读入
    val source = Source.fromFile("score.txt")
    val it = source.getLines()//迭代器
    it.next()//跳过第一行
    while (it.hasNext){
      val arr = it.next().split(",")//中文的逗号
      val name = arr(0)
      val yuwen = arr(1).toDouble
      val shuxue = arr(2).toDouble
      val yingyu = arr(3).toDouble
      val total = yuwen + shuxue +yingyu //计算总分
      val avg = total / 3  //计算平均分
      //println(name,yuwen,shuxue,yingyu,total,avg)
      val s = Student(name,yuwen,shuxue,yingyu,total,avg)
      studentList +=s
    }
    source.close()
 
    studentList.foreach(println)
 
  }
}

        (2)每个科目的平均分

import scala.collection.mutable.ListBuffer
import scala.io.Source
case class Student(name: String, yuwen: Double, shuxue: Double, yingyu: Double, total: Double, avg: Double)
//案例分析:统计成绩
object Test文件读写_成绩分析 {
  def main(args: Array[String]): Unit = {
 
    //可变List,保存索引的学生数据
    val studentList = ListBuffer[Student]()
 
    //1.按行读入
    val source = Source.fromFile("score.txt")
    val it = source.getLines()//迭代器
    it.next()//跳过第一行
    while (it.hasNext){
      val arr = it.next().split(",")//中文的逗号
      val name = arr(0)
      val yuwen = arr(1).toDouble
      val shuxue = arr(2).toDouble
      val yingyu = arr(3).toDouble
      val total = yuwen + shuxue +yingyu //计算总分
      val avg = total / 3  //计算平均分
      //println(name,yuwen,shuxue,yingyu,total,avg)
      val s = Student(name,yuwen,shuxue,yingyu,total,avg)
      studentList +=s
    }
    source.close()
 
    //计算单科总分
    var avgYuwen:Double = 0
    var avgShuxue:Double = 0
    var avgYingyu:Double = 0
    studentList.foreach(s=>{
      avgYuwen += s.yuwen
      avgShuxue += s.shuxue
      avgYingyu +=s.yingyu
    })
    println("语文平均分",avgYuwen/studentList.length)
    println("数学平均分",avgShuxue/studentList.length)
    println("英语平均分",avgYingyu/studentList.length)
 
  }
}

        (3)总分前三名

import scala.collection.mutable.ListBuffer
import scala.io.Source
case class Student(name: String, yuwen: Double, shuxue: Double, yingyu: Double, total: Double, avg: Double)
//案例分析:统计成绩
object Test文件读写_成绩分析 {
  def main(args: Array[String]): Unit = {
 
    //可变List,保存索引的学生数据
    val studentList = ListBuffer[Student]()
 
    //1.按行读入
    val source = Source.fromFile("score.txt")
    val it = source.getLines()//迭代器
    it.next()//跳过第一行
    while (it.hasNext){
      val arr = it.next().split(",")//中文的逗号
      val name = arr(0)
      val yuwen = arr(1).toDouble
      val shuxue = arr(2).toDouble
      val yingyu = arr(3).toDouble
      val total = yuwen + shuxue +yingyu //计算总分
      val avg = total / 3  //计算平均分
      //println(name,yuwen,shuxue,yingyu,total,avg)
      val s = Student(name,yuwen,shuxue,yingyu,total,avg)
      studentList +=s
    }
    source.close()
 
    //计算单科总分
    var avgYuwen:Double = 0
    var avgShuxue:Double = 0
    var avgYingyu:Double = 0
    studentList.foreach(s=>{
      avgYuwen += s.yuwen
      avgShuxue += s.shuxue
      avgYingyu +=s.yingyu
    })
    println("语文平均分",avgYuwen/studentList.length)
    println("数学平均分",avgShuxue/studentList.length)
    println("英语平均分",avgYingyu/studentList.length)
 
    //总分前三名
    //思路:1.对所有的同学按总分从高到低排名
    val list1 =studentList.sortWith((a,b)=>a.total > b.total).slice(0,3)
    println("总分从高到低排名前三:")
    list1.foreach(println)
    
    
  }
}

        (4)单科的前三名

package scala_mianx
 
import scala.collection.mutable.ListBuffer
import scala.io.Source
case class Student(name: String, yuwen: Double, shuxue: Double, yingyu: Double, total: Double, avg: Double)
//案例分析:统计成绩
object Test文件读写_成绩分析 {
  def main(args: Array[String]): Unit = {
 
    //可变List,保存索引的学生数据
    val studentList = ListBuffer[Student]()
 
    //1.按行读入
    val source = Source.fromFile("score.txt")
    val it = source.getLines()//迭代器
    it.next()//跳过第一行
    while (it.hasNext){
      val arr = it.next().split(",")//中文的逗号
      val name = arr(0)
      val yuwen = arr(1).toDouble
      val shuxue = arr(2).toDouble
      val yingyu = arr(3).toDouble
      val total = yuwen + shuxue +yingyu //计算总分
      val avg = total / 3  //计算平均分
      //println(name,yuwen,shuxue,yingyu,total,avg)
      val s = Student(name,yuwen,shuxue,yingyu,total,avg)
      studentList +=s
    }
    source.close()
 
    //计算单科总分
    var avgYuwen:Double = 0
    var avgShuxue:Double = 0
    var avgYingyu:Double = 0
    studentList.foreach(s=>{
      avgYuwen += s.yuwen
      avgShuxue += s.shuxue
      avgYingyu +=s.yingyu
    })
    println("语文平均分",avgYuwen/studentList.length)
    println("数学平均分",avgShuxue/studentList.length)
    println("英语平均分",avgYingyu/studentList.length)
 
    //总分前三名
    //思路:1.对所有的同学按总分从高到低排名
    val list1 =studentList.sortWith((a,b)=>a.total > b.total).slice(0,3)
    println("总分从高到低排名前三:")
    list1.foreach(println)
 
    //单科成绩前三名
    //语文成绩前三名
    val list2 = studentList.sortWith((a,b)=> a.yuwen>b.yuwen).slice(0,3)
    println("语文总分从高到低排名前三:")
    list2.foreach(println)
    //数学成绩前三名
    val list3 = studentList.sortWith((a,b)=> a.shuxue>b.shuxue).slice(0,3)
    println("数学总分从高到低排名前三:")
    list3.foreach(println)
    //英语成绩前三名
    val list4 = studentList.sortWith((a,b)=> a.yingyu>b.yingyu).slice(0,3)
    println("英语总分从高到低排名前三:")
    list4.foreach(println)
  }
}

————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/2301_82251033/article/details/144017940

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值