Scala文件读写——成绩分析

根据文件解决下例问题

1.读入txt文件:按行读入

Scala 复制代码
import 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)计算每个同学的总分和平均分

Scala 复制代码
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)每个科目的平均分

Scala 复制代码
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)总分前三名

Scala 复制代码
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)单科的前三名

Scala 复制代码
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)
  }
}

3.把结果写入文件

Scala 复制代码
package scala_mianx

import java.io.PrintWriter
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)

    //写入文件
    val writer=new PrintWriter("统计成绩的结果")
    writer.println("姓名,语文,数学,英语,总分,平均分")
    studentList.foreach(s =>{
      val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
      writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
    })
    writer.println("-"*40)

    writer.close()  //关闭文件
  }
}

代码整合

Scala 复制代码
import java.io.PrintWriter
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)

    //写入文件
    val writer=new PrintWriter("统计成绩的结果")
    writer.println("姓名,语文,数学,英语,总分,平均分")
    studentList.foreach(s =>{
      val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
      writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
    })
    writer.println("-"*40)
    //总分前三名
    list1.foreach(s=>{
    val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
    writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
    })
    writer.println("-"*40)
    //语文前三名
    list2.foreach(s=>{
    val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
    writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
    })
    writer.println("-"*40)
    //数学前三名
    list3.foreach(s=>{
    val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
    writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
    })
    writer.println("-"*40)
    //英语前三名
    list4.foreach(s=>{
      val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
      writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
    })
    writer.println("-"*40)

    writer.close()  //关闭文件
  }
}

运行结果:

任务说明

在本课内容的基础上,补充两门新课(Java,scala)的成绩,并完成

1.总分和平均分的计算

2.单科成绩的前5名排名

3.每个学生的在班级的整体排名

Scala 复制代码
import java.io.PrintWriter
import scala.collection.mutable.ListBuffer
import scala.io.Source
case class Student(name: String, yuwen: Double, shuxue: Double, yingyu: Double,java:Double,scala: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 java = arr(4).toDouble
      val scala = arr(5).toDouble
      val total = yuwen + shuxue +yingyu //计算总分
      val avg = total / 5  //计算平均分
      //println(name,yuwen,shuxue,yingyu,total,avg)
      val s = Student(name,yuwen,shuxue,yingyu,java,scala,total,avg)
      studentList +=s
    }
    source.close()

    //计算单科总分
    var avgYuwen:Double = 0
    var avgShuxue:Double = 0
    var avgYingyu:Double = 0
    var avgJava:Double = 0
    var avgScala:Double = 0
    studentList.foreach(s=>{
      avgYuwen += s.yuwen
      avgShuxue += s.shuxue
      avgYingyu +=s.yingyu
      avgJava +=s.java
      avgScala +=s.scala
    })
    println("语文平均分",avgYuwen/studentList.length)
    println("数学平均分",avgShuxue/studentList.length)
    println("英语平均分",avgYingyu/studentList.length)
    println("java平均分",avgJava/studentList.length)
    println("scala平均分",avgScala/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("语文总分从高到低排名前3:")
    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)
    //java成绩前5名
    val list5 = studentList.sortWith((a,b)=> a.java>b.java).slice(0,5)
    println("英语总分从高到低排名前三:")
    list4.foreach(println)
    //scala成绩前5名
    val list6 = studentList.sortWith((a,b)=> a.scala>b.scala).slice(0,5)
    println("英语总分从高到低排名前三:")
    list4.foreach(println)

    //写入文件
    val writer=new PrintWriter("统计成绩的结果")
    writer.println("姓名,语文,数学,英语,java,scala总分,平均分")
    studentList.foreach(s =>{
      val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
      writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.java},${s.scala},${s.total},${avg}")
    })
    writer.println("-"*40)
    //总分前三名
    list1.foreach(s=>{
    val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
    writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.java},${s.scala},${s.total},${avg}")
    })
    writer.println("-"*40)
    //语文前三名
    list2.foreach(s=>{
    val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
    writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.java},${s.scala},${s.total},${avg}")
    })
    writer.println("-"*40)
    //数学前三名
    list3.foreach(s=>{
    val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
    writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.java},${s.scala},${s.total},${avg}")
    })
    writer.println("-"*40)
    //英语前三名
    list4.foreach(s=>{
      val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
      writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.java},${s.scala},${s.total},${avg}")
    })
    writer.println("-"*40)
    //java前三名
    list5.foreach(s=>{
      val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
      writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.java},${s.scala},${s.total},${avg}")
    })
    writer.println("-"*40)
    //scala前三名
    list6.foreach(s=>{
      val avg =String.format("%.1f",s.avg)  //只保留一位有效数字
      writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.java},${s.scala},${s.total},${avg}")
    })
    writer.println("-"*40)

    writer.close()  //关闭文件
  }
}
相关推荐
是阿威啊1 天前
【用户行为归因分析项目】- 【企业级项目开发第五站】数据采集并加载到hive表
大数据·数据仓库·hive·hadoop·spark·scala
是阿威啊4 天前
【用户行为归因分析项目】- 【企业级项目开发第四站】模拟三类用户行为数据上传到Hadoop
大数据·hadoop·分布式·sql·scala
今天没有盐6 天前
Python字符串操作全解析:从基础定义到高级格式化
后端·scala·编程语言
是阿威啊7 天前
【用户行为归因分析项目】- 【企业级项目开发第一站】项目架构和需求设计
大数据·hive·hadoop·架构·spark·scala
代码于老总9 天前
【Scala 技巧】用隐式类给 String “开挂”:一行代码实现手机号 / 身份证号校验
scala
scala舔狗汪9 天前
scala的隐式对象和隐式类
scala
是阿威啊10 天前
【maap-analysis】spark离线数仓项目完整的开发流程
大数据·分布式·spark·scala
豚踢兔x15 天前
正则表达式应用-手机号打码
scala
代码于老总16 天前
正则表达式在 Scala 中的应用
scala
凯新生物22 天前
聚乙二醇二生物素,Biotin-PEG-Biotin在生物检测中的应用
scala·bash·laravel·perl