无法在 Scala 中编译和 运行 多个文件
Cannot compile and run multiple files in Scala
我是 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))
}
}
如果您想参考来源,link 在这里:
https://github.com/vishallama/programming-in-scala-3rd-ed/tree/master/src/chap10
我试图通过键入以下内容使其成为字节码:
scalac *.scala
它将在当前目录下创建一个 chap10 文件夹
然后我尝试使用
scala chap10.main
它returns
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 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
全部错误。
我是 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))
}
}
如果您想参考来源,link 在这里: https://github.com/vishallama/programming-in-scala-3rd-ed/tree/master/src/chap10
我试图通过键入以下内容使其成为字节码:
scalac *.scala
它将在当前目录下创建一个 chap10 文件夹
然后我尝试使用
scala chap10.main
它returns
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 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
全部错误。