标签:input original student split nbsp ase 统计学 表头 意思
package com.bjsxt.scala object Lesson_ClassAndObj { def main(args:Array[String]) { var ii=1 do{ println("样例"+ii+":") val inputFile=scala.io.Source.fromFile("test"+ii+".txt") val originalData=inputFile.getLines.map{_.split{"\\s+"}}.toList val courseNames=originalData.head.drop(2) val allStudents=originalData.tail val courseNum=courseNames.length def statistc(lines:List[Array[String]])= { (for(i<- 2 to courseNum+1) yield { val temp =lines map { elem=>elem(i).toDouble } (temp.sum,temp.min,temp.max) })map{case(total,min,max)=>(total/lines.length,min,max)} //最后一个map对for的结果进行修改,将总分转为平均分 } def printResult(theresult:Seq[(Double,Double,Double)]) { (courseNames zip theresult)foreach { case(course,result)=> println(f"${course+":"}%-10s${result._1}%5.2f${result._2}%8.2f${result._3}%8.2f") } } val allResult=statistc(allStudents) println("course average min max") printResult(allResult) val (maleLines,femaleLines)=allStudents partition { _(1)=="male" } val maleResult=statistc(maleLines) println("course average min max (males)") printResult(maleResult) val femaleResult=statistc(femaleLines) println("course average min max (females)") printResult(femaleResult) ii=ii+1 println() }while(ii!=3) }}
标签:input original student split nbsp ase 统计学 表头 意思
原文地址:https://www.cnblogs.com/xuange1/p/12271200.html