Scala文件读写——成绩分析

根据文件解决下例问题

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

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)计算每个同学的总分和平均分

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)
  }
}

3.把结果写入文件

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()  //关闭文件
  }
}

代码整合

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.每个学生的在班级的整体排名

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()  //关闭文件
  }
}

上一篇:《解锁计算机专业宝藏:核心编程语言与学习资料全解析》


下一篇:鉴于很多笔记本笔记不安全,手机下载安全开源笔记本的方法