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。
import scala.io.StdIn object test_demo { def main(args: Array[String]) { while(2>1){ var Sn=0.0 println("请输入n") var n:Long=StdIn.readLine().toLong var i=1.0 while(Sn<n) { Sn=Sn+(i+1)/i i=i+1 } println("计算级数为:",Sn) } } }
运行结果:
2. 模拟图形绘制
case class Point(var x:Double,var y:Double)extends Drawable { def shift(deltaX:Double,deltaY:Double) { x+=deltaX y+=deltaY } } trait Drawable { def draw(){println(this.toString)} } abstract class Shape(var location:Point)//location是Shape的一个可变字段 { def moveTo(newLocation:Point)//默认实现,只是修改位置 { location=newLocation } def zoom(scale:Double) } class Line(beginPoint:Point,var endPoint:Point)extends Shape(beginPoint)with Drawable { override def draw() { println(s"Line:(${location.x},${location.y})--(${endPoint.x},${endPoint.y})")//按规定格式重载click } override def moveTo(newLocation:Point) { endPoint.shift(newLocation.x-location.x,newLocation.y-location.y)//直线移动时,先移动另外一个端点 location=newLocation //移动位置 } override def zoom(scale:Double) { val midPoint=Point((endPoint.x+location.x)/2,(endPoint.y+location.y)/2) //求出中点,并按中点进行缩放 location.x=midPoint.x+scale*(location.x-midPoint.x) location.y=midPoint.y+scale*(location.y-midPoint.y) endPoint.x=midPoint.x+scale*(endPoint.x-midPoint.x) endPoint.y=midPoint.y+scale*(endPoint.y-midPoint.y) } } class Circle(center:Point,var radius:Double)extends Shape(center)with Drawable { override def draw() //按规定重载click { println(s"Circle center:(${location.x},${location.y}),R=$radius") } override def zoom(scale:Double) { radius=radius*scale //对圆的缩放只用修改半径 } } object MyDraw { def main(args:Array[String]) { val p=Point(10,30) p.draw() val line1=new Line(Point(0,0),Point(20,20)) line1.draw() line1.moveTo(Point(5,5)) line1.draw() line1.zoom(2) line1.draw() val cir=new Circle(Point(10,10),5) cir.draw() cir.moveTo(Point(30,20)) cir.draw() cir.zoom(0.5) cir.draw() } }
运行结果:
3. 统计学生成绩
import scala.collection.mutable.ArrayBuffer object Operate{ def main(args:Array[String]){ var students:ArrayBuffer[Student] = new ArrayBuffer[Student]() students.append(new Student(301610, "male", 80, 64, 78)) students.append(new Student(301611, "female", 65, 87, 58)) students.append(new Student(301612, "female", 44, 71, 77)) students.append(new Student(301613, "female", 66, 71, 91)) students.append(new Student(301614, "female", 70, 71, 100)) students.append(new Student(301615, "male", 72, 77, 72)) students.append(new Student(301616, "female", 73, 81, 75)) students.append(new Student(301617, "female", 69, 77, 75)) students.append(new Student(301618, "male", 73, 61, 65)) students.append(new Student(301619, "male", 74, 69, 68)) students.append(new Student(301620, "male", 76, 62, 76)) students.append(new Student(301621, "male", 73, 69, 91)) students.append(new Student(301622, "male", 55, 69, 61)) students.append(new Student(301623, "male", 50, 58, 75)) students.append(new Student(301624, "female", 63, 83, 93)) students.append(new Student(301625, "male", 72, 54, 100)) students.append(new Student(301626, "male", 76, 66, 73)) students.append(new Student(301627, "male", 82, 87, 79)) students.append(new Student(301628, "female", 62, 80, 54)) students.append(new Student(301629, "male", 89, 77, 72)) println(students.length) println(students(1).toString) getData(students) getDataMale(students) getDataFeMale(students) } def getData(value: ArrayBuffer[Student]){ var Mathaverage:Double=0 var MathSum:Double = 0 var MathMin:Double = value(0).Math var MathMax:Double = 0 var Englishaverage:Double=0 var EnglishSum:Double = 0 var EnglishMin:Double = value(0).English var EnglishMax:Double = 0 var Physicsaverage:Double=0 var PhysicsSum:Double = 0 var PhysicsMin:Double = value(0).Physics var PhysicsMax:Double = 0 var number = value.length for (i <- 0 until value.length){ MathSum = value(i).Math+MathSum EnglishSum = value(i).English+EnglishSum PhysicsSum = value(i).Physics+PhysicsSum if(value(i).Math>MathMax){ MathMax=value(i).Math } if(value(i).English>EnglishMax){ EnglishMax=value(i).English } if(value(i).Physics>PhysicsMax){ PhysicsMax=value(i).Physics } if(value(i).Math<MathMin){ MathMin=value(i).Math } if(value(i).English<EnglishMin){ EnglishMin=value(i).English } if(value(i).Physics<PhysicsMin){ PhysicsMin=value(i).Physics } } Mathaverage=MathSum/number Englishaverage=EnglishSum/number Physicsaverage=PhysicsSum/number println("course"+" "+"average"+" "+"min"+" "+"max"+" ") println("Math : "+Mathaverage+" "+MathMin+" "+MathMax) println("English: "+Englishaverage+" "+EnglishMin+" "+EnglishMax) println("Physics: "+Physicsaverage+" "+PhysicsMin+" "+PhysicsMax) } def getDataMale(value: ArrayBuffer[Student]){ var Mathaverage: Double = 0 var MathSum: Double = 0 var MathMin: Double = value(0).Math var MathMax: Double = 0 var Englishaverage: Double = 0 var EnglishSum: Double = 0 var EnglishMin: Double = value(0).English var EnglishMax: Double = 0 var Physicsaverage: Double = 0 var PhysicsSum: Double = 0 var PhysicsMin: Double = value(0).Physics var PhysicsMax: Double = 0 var number = 0 for (i <- 0 until value.length) { if (value(i).gender.equals("male")) { number = number + 1 MathSum = value(i).Math + MathSum EnglishSum = value(i).English + EnglishSum PhysicsSum = value(i).Physics + PhysicsSum if (value(i).Math > MathMax) { MathMax = value(i).Math } if (value(i).English > EnglishMax) { EnglishMax = value(i).English } if (value(i).Physics > PhysicsMax) { PhysicsMax = value(i).Physics } if (value(i).Math < MathMin) { MathMin = value(i).Math } if (value(i).English < EnglishMin) { EnglishMin = value(i).English } if (value(i).Physics < PhysicsMin) { PhysicsMin = value(i).Physics } } } Mathaverage=MathSum/number Englishaverage=EnglishSum/number Physicsaverage=PhysicsSum/number println("course"+" "+"average"+" "+"min"+" "+"max"+" "+"(Male)") Mathaverage=Mathaverage.toInt Englishaverage=Englishaverage.toInt Physicsaverage=Physicsaverage.toInt println("Math : "+Mathaverage+" "+MathMin+" "+MathMax) println("English: "+Englishaverage+" "+EnglishMin+" "+EnglishMax) println("Physics: "+Physicsaverage+" "+PhysicsMin+" "+PhysicsMax) } def getDataFeMale(value: ArrayBuffer[Student]){ var Mathaverage:Double=0 var MathSum:Double = 0 var MathMin:Double = value(1).Math var MathMax:Double = 0 var Englishaverage:Double=0 var EnglishSum:Double = 0 var EnglishMin:Double = value(1).English var EnglishMax:Double = 0 var Physicsaverage:Double=0 var PhysicsSum:Double = 0 var PhysicsMin:Double = value(1).Physics var PhysicsMax:Double = 0 var number = 0 for (i <- 0 until value.length){ if(value(i).gender.equals("female")){ number=number+1 MathSum = value(i).Math+MathSum EnglishSum = value(i).English+EnglishSum PhysicsSum = value(i).Physics+PhysicsSum if(value(i).Math>MathMax){ MathMax=value(i).Math } if(value(i).English>EnglishMax){ EnglishMax=value(i).English } if(value(i).Physics>PhysicsMax){ PhysicsMax=value(i).Physics } if(value(i).Math<MathMin){ MathMin=value(i).Math } if(value(i).English<EnglishMin){ EnglishMin=value(i).English } if(value(i).Physics<PhysicsMin){ PhysicsMin=value(i).Physics } } } Mathaverage=MathSum/number Englishaverage=EnglishSum/number Physicsaverage=PhysicsSum/number println("course"+" "+"average"+" "+"min"+" "+"max"+" "+"(FeMale)") Mathaverage=Mathaverage.toInt Englishaverage=Englishaverage.toInt Physicsaverage=Physicsaverage.toInt println("Math : "+Mathaverage+" "+MathMin+" "+MathMax) println("English: "+Englishaverage+" "+EnglishMin+" "+EnglishMax) println("Physics: "+Physicsaverage+" "+PhysicsMin+" "+PhysicsMax) } } class Student(id1:Int,gender1:String,Math1:Double,English1:Double,Physics1:Double){ var id:Int = id1 var gender:String = gender1 var Math:Double = Math1 var English:Double = English1 var Physics:Double = Physics1 override def toString = s"Student($id, $gender, $Math, $English, $Physics)" }
运行结果: