Spark作业求学生平均成绩

实验描述

给定一组数据,通过spark编程实验实现每名学生的平均成绩,并且实现格式为: 学号:,姓名:,语文,数学:,英语:,平均成绩:的顺序。

实验条件:

安装好Idea软件,最好不要用最新版,并且在idea里插入scala插件。实验步骤:
在idea中安装scala插件:
第一步,
请添加图片描述

第二步,

请添加图片描述
最后确定就在idea里装好scala插件了,接下来就可以实验了。

接下来就是创建文件步骤:File —>New —>Project—>Maven—>…,后续只需要跟着提示走就可以了。

数据块score1.txt文件:

学号 姓名 语文 数学 英语
2001501 李小明 88 91 86
2001502 张丽 75 81 65
2001503 刘成东 62 71 58
2001504 铙刚 79 85 83
2001505 刘小小 75 62 53
2001506 王艳 88 91 93
2001507 李远 81 78 83
2001508 张东东 70 83 68
2001509 李健 77 80 85
2001510 王小燕 72 78 71

api.scala 部分:

object api{
    
    


  def main(args: Array[String]): Unit = {
    
    
    import scala.io.Source

    val  inputfile = Source.fromFile("C:\\Users\\leglon\\IdeaProjects\\che1\\src\\main\\Scala\\score1.txt") //测试样例文件的路径
    val lines = inputfile.getLines
    val Data = lines.map{
    
    _.split(" ")}
    val originalData = Data.toList
    val header = originalData.head
    val courseNames=header.drop(2)
    val courseNum = courseNames.length
    def statistic(lines:List[Array[String]])={
    
    
      for(i<-2 to courseNum+1) yield{
    
    
        val temp = lines map {
    
    
          elem => elem (i).toDouble}
        (temp.max, temp.min, temp.sum)
        def printResult(thersult: Seq[(Double,Double,Double)]): Unit={
    
    
          (courseNames zip thersult) foreach {
    
    
            case (course,result)=>println(f"${
      
      
              course + ":}%-10s${
      
      result._1}%5.2f${
      
      result._2}%8.2f${
      
      result._3}%8.2f"

              def getFailureInEnglish(): Unit = {
    
    
              }

              val allResult = statistic ( val allSudents)
              println("课程 平均成绩 最低分 最高分")
              printResult(allResult)

student.scala 部分:

class student {
    
    
  var  id:Int= _
  var  name:String= _
  var chinese:Float= _
  var math:Float= _
  var english:Float= _
  def getAverageGrade={
    
    
    ((chinese+math+english)/3)
      .formatted("%.2f")
  }
  def this(id:Int,name:String,chinese:Float,math:Float,english:Float){
    
    
    this()
    this.id=id
    this.name=name
    this.chinese=chinese
    this.math=math
    this.english=english
  }

  override def toString="学号:"+this.id+",姓名:"+this.name+",语文"+this.chinese+
    ",数学:"+this.math+",英语:"+this.english+",平均成绩:"+getAverageGrade
}
object student{
    
    
  def main (arge:Array[String]): Unit = {
    
    
    val s1 = new student(id = 2001501, name = "李小明", chinese = 88, math = 91, english = 86)
    val s2 = new student(id = 2001502, name = "张丽", chinese = 75, math = 81, english = 65)
    val s3 = new student(id = 2001503, name = "刘成东", chinese = 62, math = 71, english = 58)
    val s4 = new student(id = 2001504, name = "饶刚", chinese = 79, math = 85, english = 83)
    val s5 = new student(id = 2001505, name = "刘小小", chinese = 75, math = 62, english = 53)
    val s6 = new student(id = 2001506, name = "王艳", chinese = 88, math = 91, english = 93)
    val s7 = new student(id = 2001507, name = "李远", chinese = 81, math = 78, english = 83)
    val s8 = new student(id = 2001508, name = "张东东", chinese = 70, math = 83, english = 68)
    val s9 = new student(id = 2001509, name = "李健", chinese = 70, math = 80, english = 85)
    val s10 = new student(id = 2001510, name = "王小燕", chinese = 72, math = 78, english = 71)
    val a = Array(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10)
    a.map(println)
  }
  }

实验结果:

请添加图片描述

猜你喜欢

转载自blog.csdn.net/qq_62127918/article/details/130422739
今日推荐