【发布时间】:2017-10-05 04:29:38
【问题描述】:
在我正在进行的小型 Scala 练习中遇到了一个奇怪的编译错误。
我有这种方法,它应该一直询问用户输入,直到提供正确的答案。唉,我在模式匹配的第一个案例中被绊倒了:
override def guess(guess: Int):Unit = {
val guessIndex = binary(array, guess)
guessIndex match {
case -1 => {
val nextAttempt = StdIn.readLine(s"Please be attentive $guess is outside the search range"
+" (0 to $upperBound). Try again: \n");
val a = validateType[Int](nextAttempt)
guess(a)
}
}
}
IDE 下划线guess(a) 带有错误“Int 不接受参数”。从控制台运行sbt compile 确认此错误:
> compile
[info] Compiling 2 Scala sources to /home/vgorcinschi/Documents/eclipseProjects/Algorithms/Chapter 1 Fundamentals/algorithms1_4_34/target/scala-2.12/classes...
[error] /home/vgorcinschi/Documents/eclipseProjects/Algorithms/Chapter 1 Fundamentals/algorithms1_4_34/src/main/scala/ca/vgorcinschi/algorithms1_4_34/hotandcold/HotAndColdImpl.scala:23: Int does not take parameters
[error] guess(a)
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 0 s, completed 6-May-2017 6:47:58 PM
对于相同的错误消息,几乎没有不同的 Stackoverflow 票证,但它们适用于不同的场景。在我这里,它看起来像一个采用Int 参数的方法被拒绝。如果可以的话,请给我一个提示,这将对我有很大帮助。
【问题讨论】:
-
重命名
guess参数(或方法名称,所以它是不同的) - 参数是范围内的第一个guess,因此编译器认为您正在尝试将其称为功能。 -
嗯,这很尴尬 :-) 你想把你的评论移到一个答案上,以便我可以将其标记为正确吗?
标签: scala recursion sbt pattern-matching string-operations