欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > Scala文件读写——成绩分析

Scala文件读写——成绩分析

2024/11/30 18:10:49 来源:https://blog.csdn.net/2301_82251033/article/details/144017940  浏览:    关键词:Scala文件读写——成绩分析

根据文件解决下例问题

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

import scala.io.Sourceobject 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).toDoubleval shuxue = arr(2).toDoubleval yingyu = arr(3).toDoubleval 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).toDoubleval shuxue = arr(2).toDoubleval yingyu = arr(3).toDoubleval 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 = 0var avgShuxue:Double = 0var avgYingyu:Double = 0studentList.foreach(s=>{avgYuwen += s.yuwenavgShuxue += s.shuxueavgYingyu +=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).toDoubleval shuxue = arr(2).toDoubleval yingyu = arr(3).toDoubleval 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 = 0var avgShuxue:Double = 0var avgYingyu:Double = 0studentList.foreach(s=>{avgYuwen += s.yuwenavgShuxue += s.shuxueavgYingyu +=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_mianximport 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).toDoubleval shuxue = arr(2).toDoubleval yingyu = arr(3).toDoubleval 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 = 0var avgShuxue:Double = 0var avgYingyu:Double = 0studentList.foreach(s=>{avgYuwen += s.yuwenavgShuxue += s.shuxueavgYingyu +=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_mianximport 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).toDoubleval shuxue = arr(2).toDoubleval yingyu = arr(3).toDoubleval 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 = 0var avgShuxue:Double = 0var avgYingyu:Double = 0studentList.foreach(s=>{avgYuwen += s.yuwenavgShuxue += s.shuxueavgYingyu +=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.Sourcecase 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).toDoubleval shuxue = arr(2).toDoubleval yingyu = arr(3).toDoubleval 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 = 0var avgShuxue:Double = 0var avgYingyu:Double = 0studentList.foreach(s=>{avgYuwen += s.yuwenavgShuxue += s.shuxueavgYingyu +=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).toDoubleval shuxue = arr(2).toDoubleval yingyu = arr(3).toDoubleval java = arr(4).toDoubleval scala = arr(5).toDoubleval 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 = 0var avgShuxue:Double = 0var avgYingyu:Double = 0var avgJava:Double = 0var avgScala:Double = 0studentList.foreach(s=>{avgYuwen += s.yuwenavgShuxue += s.shuxueavgYingyu +=s.yingyuavgJava +=s.javaavgScala +=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()  //关闭文件}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com