当前位置: 首页 > article >正文

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


http://www.kler.cn/a/411176.html

相关文章:

  • QT QToolButton控件 全面详解
  • jmeter基础06_(练习)常见的http请求
  • 【c语言】文件操作详解 - 从打开到关闭
  • 【面试题】2025年百度校招Java后端面试题
  • CentOS7卸载node
  • 嵌入式的C/C++:深入理解 static、const 与 volatile 的用法与特点
  • 【AI绘画】Midjourney进阶:色调详解(上)
  • pyshark安装使用,ubuntu:20.04
  • QT6学习第四天
  • PAT甲级-1145 Average Search Time
  • C# 结构体
  • C#基础练习61-65
  • MMCM DRP动态配置方法(超详细讲解)
  • Spring Boot 3.4 正式发布,结构化日志!
  • 【Redis篇】String类型命令详讲以及它的使用场景
  • 互联网直播/点播EasyDSS视频推拉流平台视频点播有哪些技术特点?
  • 实战项目负载均衡式在线 OJ
  • Notepad++ 替换所有数字给数字加单引号
  • VITE+VUE3+TS环境搭建
  • TortoiseGit 将本地已有仓库推送到远程
  • 【RAG多模态】再看多模态RAG进行文档问答的方案
  • k8s rainbond centos7/win10 -20241124
  • java:拆箱和装箱,缓存池概念简单介绍
  • 基于springboot的HttpClient、OKhttp、RestTemplate对比
  • intellij idea控制台 visual stadio dev c++ keil pycharm python 输出乱码解决方案最终版 java
  • Springboot自带注解@Scheduled实现定时任务