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)

}

}

相关推荐
Hello_Embed4 小时前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
咸甜适中4 小时前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
Grassto5 小时前
RAG 从入门到放弃?丐版 demo 实战笔记(go+python)
笔记
Magnetic_h5 小时前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa
周周记笔记8 小时前
学习笔记:第一个Python程序
笔记·学习
丑小鸭是白天鹅8 小时前
Kotlin协程详细笔记之切线程和挂起函数
开发语言·笔记·kotlin
潘达斯奈基~8 小时前
《大数据之路1》笔记2:数据模型
大数据·笔记
..过云雨8 小时前
05.【Linux系统编程】进程(冯诺依曼体系结构、进程概念、进程状态(注意僵尸和孤儿)、进程优先级、进程切换和调度)
linux·笔记·学习
咸甜适中9 小时前
rust语言 (1.88) egui (0.32.2) 学习笔记(逐行注释)(二十八)使用图片控件显示图片
笔记·学习·rust·egui
一又四分之一.9 小时前
高数基础知识(下)②
笔记