Scala文件读写——成绩分析
根据文件办理下例问题https://i-blog.csdnimg.cn/direct/76532ca87fe34fc9a7846c1eea93c9b5.png
1.读入txt文件:按行读入
import scala.io.Source
object Test文件读写_成绩分析 {
def main(args: Array): 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): Unit = {
//可变List,保存索引的学生数据
val studentList = ListBuffer()
//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): Unit = {
//可变List,保存索引的学生数据
val studentList = ListBuffer()
//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): Unit = {
//可变List,保存索引的学生数据
val studentList = ListBuffer()
//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): Unit = {
//可变List,保存索引的学生数据
val studentList = ListBuffer()
//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): Unit = {
//可变List,保存索引的学生数据
val studentList = ListBuffer()
//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): Unit = {
//可变List,保存索引的学生数据
val studentList = ListBuffer()
//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()//关闭文件
}
}
运行效果:
https://i-blog.csdnimg.cn/direct/4d614c07e9a14954a612f088ed4a6144.png
https://i-blog.csdnimg.cn/direct/cbcd0a18e0fd4099a8a1b9411b29f2a2.png
任务分析
在本课内容的底子上,增补两门新课(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): Unit = {
//可变List,保存索引的学生数据
val studentList = ListBuffer()
//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()//关闭文件
}
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]