【发布时间】:2020-09-12 01:51:14
【问题描述】:
我是 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)
^
【问题讨论】:
-
当您的代码包含多个文件时,您将需要创建一个 Scala 项目。您可以选择任何 sbt、maven、gradle 来构建您的项目。
-
但是如果你想坚持使用普通文件,那么只需删除
package chap10。现在你可以运行scala Spiral.scala -classpath Element.scala -
谢谢Sarveshseri,我按照你的第二种方法运行命令,但它仍然抛出错误,作为我文章中的更新部分
-
我赞同使用合适的 Scala 项目的建议。
标签: scala command-line compilation scalac