我是scala新手
我在同一个目录下有两个scala示例文件
一个是:
package chap10
import Element.elem
abstract class Element {
def contents: Array[String]
def height: Int = contents.length
def width: Int =
if (height == 0) 0 else contents(0).length
def above(that: Element): Element =
elem(this.contents ++ that.contents)
def beside(that: Element): Element =
elem(
for (
(line1, line2) <- this.contents zip that.contents
) yield line1 + line2
)
private 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
}
private def heighten(h: Int): Element =
if (h <= height) this
else {
val top = elem(' ', width, (h - height) / 2)
val bottom = elem(' ', width, h - height - top.height)
top above this above bottom
}
override def toString = contents mkString "\n"
}
object Element {
private class ArrayElement(
val contents: Array[String]
) extends Element
private class LineElement(s: String) extends Element {
val contents = Array(s)
override def width = s.length
override def height = 1
}
private class UniformElement(
ch: Char,
override val width: Int,
override val height: Int
) extends Element {
private val line = ch.toString * width
def contents = Array.fill(height)(line)
}
def elem(contents: Array[String]): Element =
new ArrayElement(contents)
def elem(chr: Char, width: Int, height: Int): Element =
new UniformElement(chr, width, height)
def elem(line: String): Element =
new LineElement(line)
}另一个是
package chap10
import Element.elem
object Spiral {
val space = elem(" ")
val corner = 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]) = {
val nSides = args(0).toInt
println(spiral(nSides, 0))
}
}如果你想参考源代码,链接在这里:https://github.com/vishallama/programming-in-scala-3rd-ed/tree/master/src/chap10
我尝试通过键入以下命令将其转换为字节码:
scalac *.scala它将在当前目录下创建一个chap10文件夹
然后我试着用
scala chap10.main它返回
No such file or class on classpath: chap10.main或者
scala -classpath chap10/Spiral.main或者
scala -classpath chap10.Spiral.main所有这些对我来说都没有用。
我知道这可能是一个愚蠢的问题,但我没有太多的java或scala经验,而且我还在学习。
现在我只想让它运行
我有没有漏掉什么步骤?
谢谢大家
更新:在我删除Spiral.scala中的"package chap10“行并运行命令: scala Spiral.scala -classpath Element.scala之后,它给了我这个错误。
aaron$ scala Spiral.scala -classpath Element.scala
Spiral.scala:3: error: not found: object Element
import Element.elem
^
Spiral.scala:7: error: not found: value elem
val space = elem(" ")
^
Spiral.scala:8: error: not found: value elem
val corner = elem("+")
^
Spiral.scala:10: error: not found: type Element
def spiral(nEdges: Int, direction: Int): Element = {
^
Spiral.scala:12: error: not found: value elem
elem("+")
^
Spiral.scala:15: error: not found: value elem
def verticalBar = elem('|', 1, sp.height)
^
Spiral.scala:16: error: not found: value elem
def horizontalBar = elem('-', sp.width, 1)
^发布于 2020-09-12 11:01:49
正确的是scala chap10.Spiral
$ ls -R
.:
Element.scala Spiral.scala
$ scalac *.scala
$ ls -R
.:
chap10 Element.scala Spiral.scala
./chap10:
Element$ArrayElement.class Element$LineElement.class Spiral$.class
Element.class Element$UniformElement.class
Element$.class Spiral.class
$ scala chap10.Spiral
java.lang.ArrayIndexOutOfBoundsException: 0
at chap10.Spiral$.main(Spiral.scala:29)
...
$ scala chap10.Spiral 10
+--
| 所有scala chap10.main、scala chap10/Spiral.main、scala chap10.Spiral.main、scala Spiral.scala都是错误的。
https://stackoverflow.com/questions/63856137
复制相似问题