Scala的统计
统计成绩练习
1.计算每个同学的总分和平均分
2.统计每个科目的平均分
3.列出总分前三名和单科前三名,并保存结果到文件中
解题思路如下:
1.读入txt文件,按行读入
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)每个科目的平均分
(3)总分的前三名
(4)单科的前三名
3.把结果写入文件
完整代码如下:
package Scala_CSDN.十一月
import javafx.print.Printer
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 统计成绩 {
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 //计算平均分
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("数学平均分",avgyuwen / studentList.length)
println("英语平均分",avgyuwen / 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((c,d)=>c.shuxue > d.shuxue).slice(0,3)
println("数学从高到低排名前三:")
list3.foreach(println)
val list4 = studentList.sortWith((c,d)=>c.yingyu > d.yingyu).slice(0,3)
println("英语从高到低排名前三:")
list4.foreach(println)
//写入文件
val writer = new PrintWriter("统计成绩的结果")
writer.println("姓名,语文成绩,数学成绩,英语成绩,总分,平均分")
studentList.foreach(s=>{
val avg = f"${s.avg}%.1f" //只保留一位有效数字
writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
})
writer.println("-"*40)
//总分前三名
list1.foreach(s=>{
val avg = f"${s.avg}%.1f"
writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
})
writer.println("-"*40)
//语文前三名
list2.foreach(s=>{
val avg = f"${s.avg}%.1f" //只保留一位有效数字
writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
})
writer.println("-"*40)
//英语前三名
list3.foreach(s=>{
val avg = f"${s.avg}%.1f" //只保留一位有效数字
writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
})
writer.println("-"*40)
//数学前三名
list4.foreach(s=>{
val avg = f"${s.avg}%.1f" //只保留一位有效数字
writer.println(s"${s.name},${s.yuwen},${s.shuxue},${s.yingyu},${s.total},${avg}")
})
writer.println("-"*40)
writer.close() //关闭文件
}
}
身份证查询籍贯
根据身份证号码前两位,输出其所属籍贯:
eg:42湖北,33浙江,11北京,31上海
object Test_身份证查询籍贯 {
def main(args: Array[String]): Unit = {
val code = "42005200210030051".substring(0,2)
println(code)
//判断42是哪个省的
//湖北
// if(code == "42"){
// println("42对应省份为:湖北")
// }else if(code =="33"){
// println("浙江")
// }else{
// println()
// }
val province = code match {
case "11" => "北京"
case "12" => "天津"
case "13" => "河北"
case "14" => "山西"
case "15" => "内蒙古"
case "21" => "辽宁"
case "22" => "吉林"
case "23" => "黑龙江"
case "31" => "上海"
case "32" => "上海"
case "33" => "浙江"
case "34" => "安徽"
case "35" => "福建"
case "36" => "江西"
case "37" => "山东"
case "41" => "河南"
case "42" => "湖北"
case "43" => "湖南"
case "44" => "广东"
case "45" => "广西"
case "46" => "海南"
case "50" => "重庆"
case "51" => "四川"
case "52" => "贵州"
case "53" => "云南"
case "54" => "西藏"
case "61" => "陕西"
case "62" => "甘肃"
case "63" => "青海"
case "64" => "宁夏"
case "65" => "新疆"
case "71" => "台湾"
case "81" => "香港"
case "82" => "澳门"
case "91" => "国外"
case "_" => "未知"
}
println(province)
}
}
原文地址:https://blog.csdn.net/2401_87076500/article/details/144719037
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!