2020年寒假学习进度第二天

今天主要研究了spark实验二的第一部分的Scala编写,看完代码后发现需要在REPL模式下运行,经过查证发现需要安装scala脚本,所以尝试在虚拟机中安装scala,成功后使用给的代码尝试运行,但是出错,提示没有找到类的定义,经过查证,发现是代码中缺少类得定义。改正后可以正常运行。

import io.StdIn._
  object Test {
      def main(args: Array[String]){
          var Sn:Float = 0
  
          var n:Float=1
  
      println("please input q:")
  
     val q = readInt()
 
     while(Sn<q){
         Sn+=(n+1)/n
         n+=1
     }
 
     println(s"Sn=$Sn")
     }
 }

  

 第二个实验的第二个部分出现了这个unclosed string literal问题,百度上说的都是字符串不闭合,我仔细研究了一下代码,发现是闭合的,于是我把出问题的代码注释掉。尝试运行,发现可以的,而后我又去掉注释,就没问题了,代码就是这么神奇,格式也会有影响。

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})")
} 
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(){
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=new 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
 } }

  

 第二个实验的第三部分,经过实践,还是出现了第二部分的问题,经过调整格式实现了解决

object scoreReport{
def main(args: Array[String]) {
 // 假设数据文件在当前目录下
 val inputFile = scala.io.Source.fromFile("test.txt")
 //”\s+“是字符串正则表达式,将每行按空白字符(包括空格/制表符)分开
// 由于可能涉及多次遍历,同 toList 将 Iterator 装为 List
// originalData 的类型为 List[Array[String]]
val originalData = 
inputFile.getLines.map{_.split("\s+")} .toList 
val courseNames = originalData.head.drop(2) //获取第一行中的课程名
val allStudents = originalData.tail // 去除第一行剩下的数据
 val courseNum = courseNames.length
// 统计函数,参数为需要常用统计的行
//用到了外部变量 courseNum,属于闭包函数
def statistc(lines:List[Array[String]])= {
// for 推导式,对每门课程生成一个三元组,分别表示总分,最低分和最高分
(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)]){
// 遍历前调用 zip 方法将课程名容器和结果容器合并,合并结果为二元组容器
(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")
printResult(maleResult)
// 分别调用两个函数统计男学生并输出结果
val femaleResult = statistc(femaleLines)
println("course average min max")
printResult(femaleResult)
 } }

  

总结:做实验时遇到问题要仔细地查询解决办法,所有问题都可以解决的。

原文地址:https://www.cnblogs.com/ljm-zsy/p/12244730.html