Scala - 错误:未找到:值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18299980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:35:45  来源:igfitidea点击:

scala - error: not found: value

scalaruntime-errorworksheet

提问by nazar_art

I newly at scala and tried to pass some easy scala worksheet.

我刚加入 Scala 并试图通过一些简单的 Scala 工作表。

IDE is Intellij IDEA community edition and OS Ubuntu 12.04, sbtwas installed correctly.

IDE 是 Intellij IDEA 社区版和 OS Ubuntu 12.04,sbt已正确安装。

But it throws error - error: not found: value

但它抛出错误 - error: not found: value

OI can't understand why this happen:

OI 不明白为什么会发生这种情况:

Code:

代码:

object session {
  1 + 2
  def abs(x: Double) = if (x < 0) -x else x         <== update this line
  def sqrtIter(guess: Double, x: Double): Double =
    if (isGoodEnough(guess, x)) guess
    else sqrtIter(improve(guess, x), x)

  def isGoodEnough(guess: Double, x: Double) =
    abs(guess * guess - x) < 0.001

  def improve(guess: Double, x: Double) =
    (guess + x / guess) / 2
  def sqrt(x: Double) = sqrtIter(1.0, x)
  sqrt(2)
  sqrt(4)
}

Output from right side of screen:

屏幕右侧的输出:

> res0: Int = 3

> <console>:8: error: not found: value isGoodEnough
             if (isGoodEnough(guess, x)) guess else sqrtIter(improve(guess, x), x)  
                 ^
  <console>:8: error: not found: value improve
             if (isGoodEnough(guess, x)) guess else sqrtIter(improve(guess, x), x)  
                                                             ^
> <console>:8: error: not found: value abs
             abs(guess * guess - x) < 0.001
             ^
> improve: (guess: Double, x: Double)Double

> <console>:7: error: not found: value sqrtIter
         def sqrt(x: Double) = sqrtIter(1.0, x)
                               ^    
> <console>:8: error: not found: value sqrt
                sqrt(2)
                ^

> <console>:8: error: not found: value sqrt
                sqrt(4)

Any suggestions?

有什么建议?

回答by Shadowlands

The complete error message is error: not found: value abs. The value "abs" wan't found. You want math.abs. Alternatively you could add import math._somewhere before you need these math functions.

完整的错误信息是error: not found: value abs。未找到值“abs”。你要math.abs。或者,您可以import math._在需要这些数学函数之前在某处添加。

回答by lpm_nova

I had the same problem as you. I didn't think importing math.abs or importing math._ were valid as abs was defined in the code.

我和你有同样的问题。我不认为导入 math.abs 或导入 math._ 是有效的,因为 abs 是在代码中定义的。

What worked for me was reorganizing the order of the function definitions. You have to define something above before it can be used below.

对我有用的是重新组织函数定义的顺序。你必须在上面定义一些东西才能在下面使用它。

object session {
  def abs(x: Double) = if (x<0) - x else x

  def isGoodEnough(guess: Double, x: Double) =
  abs(guess * guess - x) < 0.001

  def improve(guess: Double, x: Double) =
    (guess + x / guess) / 2
  def sqrtIter(guess: Double, x: Double): Double =
    if(isGoodEnough(guess, x)) guess
    else sqrtIter(improve(guess, x), x)


  def sqrt(x: Double) = { sqrtIter(1.0, x) }

  sqrt(2)
  sqrt(4)
}

Returned:

回:

> abs: (x: Double)Double


> isGoodEnough: (guess: Double, x: Double)Boolean


> improve: (guess: Double, x: Double)Double


> sqrtIter: (guess: Double, x: Double)Double


> sqrt: (x: Double)Double



> res0: Double = 1.4142156862745097
> res1: Double = 2.0000000929222947

回答by nazar_art

And some easiest way without redundant code:

还有一些没有冗余代码的最简单方法:

object session {
  1 + 2
  def abs(x: Double) = if (x < 0) -x else x
  def sqrt(x: Double) = {

  def sqrtIter(guess: Double): Double =
    if (isGoodEnough(guess)) guess
    else sqrtIter(improve(guess))

  def isGoodEnough(guess: Double) =
    abs(guess * guess - x) / x < 0.001

  def improve(guess: Double) =
    (guess + x / guess) / 2
  sqrtIter(1.0)
  }
  sqrt(2)
  sqrt(4)
  sqrt(1e-16)
  sqrt(1e60)
}