Scala初级实践
1. 计算级数
请用脚本的方式编程计算并输出下列级数的前 n 项之和 Sn,直到 Sn 刚好大于或等于 q
为止,其中 q 为大于 0 的整数,其值通过键盘输入。
例 如 , 若 q 的 值 为 50.0 , 则 输 出 应 为 : Sn=50.416695 。 请 将 源 文 件 保 存 为
exercise2-1.scala,在REPL模式下测试运行,测试样例:q=1时,Sn=2;q=30时,Sn=30.891459;
q=50 时,Sn=50.416695。
package com.atguigu.scala.chapter07 import scala.io.StdIn object test1 { def main(args: Array[String]): Unit = { val i: Int = StdIn.readInt() var sum:Double=0.0 for (j<- 1 to i) { val i1: Double = (j + 1)*1.0 / j // println(i1) sum=sum+i1 println(s"结果为${sum}其中i为${j}和${i1}") } println(sum) } }
3. 统计学生成绩
学生的成绩清单格式如下所示,第一行为表头,各字段意思分别为学号、性别、课程名
1、课程名 2 等,后面每一行代表一个学生的信息,各字段之间用空白符隔开
Id
gender Math English Physics
301610 male 80 64 78
301611 female 65 87 58
...
给定任何一个如上格式的清单(不同清单里课程数量可能不一样),要求尽可能采用函
数式编程,统计出各门课程的平均成绩,最低成绩,和最高成绩;另外还需按男女同学分开,
分别统计各门课程的平均成绩,最低成绩,和最高成绩。
测试样例 1 如下:
Id
gender Math English Physics
301610 male 80 64 78
301611 female 65 87 58
301612 female 44 71 77
301613 female 66 71 91
301614 female 70 71 100
301615 male 72 77 72
301616 female 73 81 75
301617 female 69 77 75
301618 male 73 61 65
301619 male 74 69 68
301620 male 76 62 76
301621 male 73 69 91
301622 male 55 69 61
301623 male 50 58 75
301624 female 63 83 93
301625 male 72 54 100
301626 male 76 66 73
301627 male 82 87 79
301628 female 62 80 54
301629 male 89 77 72
样例 1 的统计结果输出为:
course average min max
Math: 69.20 44.00 89.00
English: 71.70 54.00 87.00
Physics: 76.65 54.00 100.00
course average min max (males)
Math: 72.67 50.00 89.00
English: 67.75 54.00 87.00厦门大学林子雨,赖永炫,陶继平 编著《Spark 编程基础(Scala 版)》 教材配套机房上机实验指南
实验 2 Scala 编程初级实践
主讲教师:林子雨 http://www.cs.xmu.edu.cn/linziyu 第 4 页
Physics: 75.83 61.00 100.00
course average min max (females)
Math: 64.00 44.00 73.00
English: 77.63 71.00 87.00
Physics: 77.88 54.00 100.00
package com.atguigu.scala.shiyan object shiyan03 { def main(args: Array[String]): Unit = { val students: List[Student] = List(Student(301610, "male", 80, 64, 78), Student(301611, "female", 65, 87, 58), Student(301612, "female", 44, 71, 77),Student(301613 ,"female" ,66 ,71 ,91), Student(301614, "female", 70, 71, 100) ,Student(301615, "male", 72, 77, 72) ,Student(301616, "female", 73, 81, 75) ,Student(301617, "female", 69, 77, 75) ,Student(301618, "male", 73, 61, 65) ,Student(301619, "male", 74, 69, 68) ,Student(301620, "male", 76, 62, 76) ,Student(301621, "male", 73, 69, 91) ,Student(301622, "male", 55, 69, 61) ,Student(301623, "male", 50, 58, 75) ,Student(301624, "female", 63, 83, 93) ,Student(301625, "male", 72, 54, 100) ,Student(301626, "male", 76, 66, 73) ,Student(301627, "male", 82, 87, 79) ,Student(301628, "female", 62, 80, 54) ,Student(301629, "male", 89, 77, 72)) val allmath: Student = students.maxBy(f => { f.Math }) val allmathmin: Student = students.minBy(f => { f.Math }) val allenglishmax: Student = students.maxBy(f => { f.English }) val allengslihmin: Student = students.minBy(f => { f.English }) val allchinesemax: Student = students.maxBy(f => { f.Chinese }) val allchinesxmin: Student = students.minBy(f => { f.Chinese }) val stringToStudents: Map[String, List[Student]] = students.groupBy(f => { f.gender }) val maleStudent: List[Student] = stringToStudents("male") val femaleStudent: List[Student] = stringToStudents("female") val femalemathmax: Student = femaleStudent.maxBy(f=>{ f.Math }) val femalemathmin: Student = femaleStudent.minBy(f=>{ f.Math }) val femaleenglishmax: Student = femaleStudent.maxBy(f => { f.English }) val femalemenglishmin: Student = femaleStudent.minBy(f => { f.English }) val femalechinesemax: Student = femaleStudent.maxBy(f => { f.Chinese }) val femalechinesxmin: Student = femaleStudent.minBy(f => { f.Chinese }) val malemathmax: Student = maleStudent.maxBy(f=>{ f.Math }) val malemathmin: Student = maleStudent.minBy(f=>{ f.Math }) val maleenglishmax: Student = maleStudent.maxBy(f => { f.English }) val maleenglishmin: Student = maleStudent.minBy(f => { f.English }) val malechinesemax: Student = maleStudent.maxBy(f => { f.Chinese }) val malechinesxmin: Student = maleStudent.minBy(f => { f.Chinese }) println("course min max") println(s"Math:${allmathmin.Math} ${allmath.Math}") println(s"English:${allengslihmin.English} ${allenglishmax.English}") println(s"Chinese:${allchinesxmin.Chinese} ${allchinesemax.Chinese}") println("course min max(female)") println(s"Math:${femalemathmin.Math} ${femalemathmax.Math}") println(s"English:${femalemenglishmin.English} ${femaleenglishmax.English}") println(s"Chinese:${femalechinesxmin.Chinese} ${femalechinesemax.Chinese}") println("course min max(male)") println(s"Math:${malemathmin.Math} ${malemathmax.Math}") println(s"English:${maleenglishmin.English} ${maleenglishmax.English}") println(s"Chinese:${malechinesxmin.Chinese} ${malechinesemax.Chinese}") } } object Student{ def apply(Id:Int,gender:String,Math:Int,English:Int,Chinese:Int): Student = new Student(Id,gender,Math,English,Chinese) } class Student{ var Id:Int= _ var gender:String= _ var Math:Int=_ var English:Int=_ var Chinese:Int=_ def this(Id:Int,gender:String,Math:Int,English:Int,Chinese:Int) { this() this.Id=Id this.gender=gender this.Math=Math this.English=English this.Chinese=Chinese } }