这是用Scala编写的代码:
for (i <- 0 until 10) {
if (i > 0) {
val new = theta(i * 5)
}
// using variable new
val theta = DenseVector.zeros[Int]((i + 1) * 10)
// doing operations on theta
}
每次迭代都有自己的变量,变量的顺序不会改变,因为它们之间做了一些操作。
当我运行此代码时,它会显示以下错误:
Wrong forward reference
我该如何解决这个问题?
答案 0 :(得分:1)
通过theta
方法替换if
表达式之前的theta
。
def theta(i: Int) = DenseVector.zeros[Int]((i + 1) * 10)
for (i <- 0 until 10) {
if (i > 0) {
val new = theta(i * 5)
}
// doing operations on theta
}