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)

}

}

相关推荐
bnsarocket1 小时前
Verilog和FPGA的自学笔记6——计数器(D触发器同步+异步方案)
笔记·fpga开发·verilog·自学·硬件编程
LK_072 小时前
【Open3D】Ch.3:顶点法向量估计 | Python
开发语言·笔记·python
li星野2 小时前
打工人日报#20251011
笔记·程序人生·fpga开发·学习方法
摇滚侠2 小时前
Spring Boot 3零基础教程,yml配置文件,笔记13
spring boot·redis·笔记
QT 小鲜肉3 小时前
【个人成长笔记】在Ubuntu中的Linux系统安装 anaconda 及其相关终端命令行
linux·笔记·深度学习·学习·ubuntu·学习方法
QT 小鲜肉3 小时前
【个人成长笔记】在Ubuntu中的Linux系统安装实验室WIFI驱动安装(Driver for Linux RTL8188GU)
linux·笔记·学习·ubuntu·学习方法
急急黄豆3 小时前
MADDPG学习笔记
笔记·学习
Chloeis Syntax3 小时前
栈和队列笔记2025-10-12
java·数据结构·笔记·
QZ_orz_freedom4 小时前
学习笔记--文件上传
java·笔记·学习
摇滚侠4 小时前
Spring Boot 3零基础教程,整合Redis,笔记12
spring boot·redis·笔记