首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从“Scala第三版编程”第10章中获得StackOverFlowError

从“Scala第三版编程”第10章中获得StackOverFlowError
EN

Stack Overflow用户
提问于 2019-11-21 09:04:29
回答 1查看 107关注 0票数 0

我正在处理"scala第三版编程“第10章中的类元素和螺旋示例。现在我尝试使用”Scala螺旋8“运行它,并得到了错误:

线程"main“java.lang.StackOverflowError中的异常

代码语言:javascript
复制
 at java.base/java.lang.String.(String.java:250)
代码语言:javascript
复制
 at java.base/java.lang.String.valueOf(String.java:2802)
代码语言:javascript
复制
 at java.base/java.lang.Character.toString(Character.java:7563)
代码语言:javascript
复制
 at Element$UniformElement.(Element.scala:9)
代码语言:javascript
复制
 at Element$.elem(Element.scala:24)
代码语言:javascript
复制
 at Element.heighten(Element.scala:66)
代码语言:javascript
复制
 at Element.beside(Element.scala:46)
代码语言:javascript
复制
 at Element.widen(Element.scala:59)

等等..。

我对Scala还不熟悉,为了不感到沮丧和改进,所以如果您想清楚了,请尽可能地描述一下您是如何发现这个错误的,以及它是什么。谢谢!

下面是代码:

代码语言:javascript
复制
object Element {
  private class ArrayElement(val contents: Array[String]) extends Element

  private class UniformElement(
                                ch: Char,
                                override val height: Int,
                                override val width: Int
                              ) extends Element {
    private val line = ch.toString * width
    def contents: Array[String] = Array.fill(height)(line)
  }

  private class LineElement(s: String) extends Element {
    val contents = Array(s)
    override def width: Int = s.length
    override def height = 1
  }

  def elem(contents: Array[String]): Element = {
    new ArrayElement(contents)
  }

  def elem(ch: Char, height: Int, width: Int): Element = {
    new UniformElement(ch, height, width)
  }

  def elem(line: String): Element = {
    new LineElement(line)
  }
}

import Element.elem
abstract class Element {
  def contents: Array[String]

  def width: Int = contents(0).length
  def height: Int = contents.length

  def above(that: Element): Element = {
    val this1 = this widen that.width
    val that1 = that widen this.width
    elem(this1.contents ++ that1.contents)
  }

  def beside(that: Element): Element = {
    val this1 = this heighten that.height
    val that1 = that heighten this.height
    elem(
      for ((line1, line2) <- this1.contents zip that1.contents)
        yield line1 + line2
    )
  }

  def widen(w: Int): Element = {
    if (w <= width) this
    else {
      val left = elem(' ', (w - width) / 2, height)
      val right = elem(' ', w - width - left.width, height)
      left beside this beside right
    }
  }

  def heighten(h: Int): Element = {
    if (h <= height) this
    else {
      val top = elem(' ', width, (h - height) / 2)
      val bot = elem(' ', width, h - height - top.height)
      top above this above bot
    }
  }

  override def toString: String = contents mkString "\n"
}
代码语言:javascript
复制
import Element.elem
object Spiral {
  val space: Element = elem(" ")
  val corner: Element = elem("+")

  def spiral(nEdges: Int, direction: Int): Element = {
    if (nEdges == 1)
      elem("+")
    else {
      val sp = spiral(nEdges -1, (direction + 3) % 4)
      def verticalBar = elem('|', 1, sp.height)
      def horizontalBar = elem('-', sp.width, 1)
      if (direction == 0)
        (corner beside horizontalBar) above (sp beside space)
      else if (direction == 1)
        (sp above space) beside (corner above verticalBar)
      else if (direction == 2)
        (space beside sp) above (horizontalBar beside corner)
      else
        (verticalBar above corner) beside (space above sp)
    }
  }

  def main(args: Array[String]): Unit = {
    val nSides = args(0).toInt
    println(spiral(nSides, 0))
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-21 11:12:46

您刚刚在widthelem中倒置了UniformElementheight参数的顺序。因此,当您从elemwiden调用heighten时,您传递了高度的宽度,反之亦然。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58970981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档