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)

}

}

相关推荐
一只侯子7 小时前
Face AE Tuning
图像处理·笔记·学习·算法·计算机视觉
whale fall9 小时前
【剑雅14】笔记
笔记
星空的资源小屋11 小时前
跨平台下载神器ArrowDL,一网打尽所有资源
javascript·笔记·django
Xudde.11 小时前
Quick2靶机渗透
笔记·学习·安全·web安全·php
AA陈超12 小时前
Git常用命令大全及使用指南
笔记·git·学习
愚戏师13 小时前
Python3 Socket 网络编程复习笔记
网络·笔记
降临-max13 小时前
JavaSE---网络编程
java·开发语言·网络·笔记·学习
大白的编程日记.14 小时前
【计算网络学习笔记】MySql的多版本控制MVCC和Read View
网络·笔记·学习·mysql
IMPYLH15 小时前
Lua 的 require 函数
java·开发语言·笔记·后端·junit·lua
YJlio18 小时前
进程和诊断工具学习笔记(8.29):ListDLLs——一眼看清进程里加载了哪些 DLL,谁在偷偷注入
android·笔记·学习