Scala学习记录,统计成绩

统计成绩练习

1.计算每个同学的总分和平均分

2.统计每个科目的平均分

3.列出总分前三名和单科前三名,并保存结果到文件中

解题思路如下:

1.读入txt文件,按行读入

2.处理数据

(1)计算每个同学的总分平均分

Scala 复制代码
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.把结果写入文件

完整代码如下:

Scala 复制代码
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上海

Scala 复制代码
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)
  }
}

根据输出的数字输出对应 的星期的英文

算出正方形,长方形,梯形面积

相关推荐
Slow菜鸟6 小时前
AI学习篇(五) | awesome-design-md 使用说明
人工智能·学习
狐狐生风6 小时前
LangChain 向量存储:Chroma、FAISS
人工智能·python·学习·langchain·faiss·agentai
狐狐生风7 小时前
LangChain RAG 基础
人工智能·python·学习·langchain·rag·agentai
努力努力再努力FFF9 小时前
医生对AI辅助诊断感兴趣,作为临床人员该怎么了解和学习?
人工智能·学习
sakiko_10 小时前
UIKit学习笔记5-使用UITableView制作聊天页面
笔记·学习·swift·uikit
Alice-YUE11 小时前
【js高频八股】防抖与节流
开发语言·前端·javascript·笔记·学习·ecmascript
北山有鸟12 小时前
修改源码法和插件法
嵌入式硬件·学习
richxu2025100112 小时前
嵌入式学习之路->stm32篇->(14)通用定时器(上)
stm32·单片机·嵌入式硬件·学习
qeen8712 小时前
【数据结构】建堆的时间复杂度讨论与TOP-K问题
c语言·数据结构·c++·学习·
lizhihai_9913 小时前
股市学习心得-六张分时保命图
大数据·人工智能·学习