我是一個斯卡拉初學者。我在中看到了這個代碼。STEP 7.具有類型的參數化陣列 of 用Scala編程,第3版2。爲什麼變量不需要在Scala代碼中定義?
val greetStrings = new Array[String](3)
greetStrings(0) = "Hello"
greetStrings(1) = ", "
greetStrings(2) = "world!\n"
for (i <- 0 to 2)
print(greetStrings(i))
我不明白「我」不需要定義。當我寫這樣的一些代碼:出現
val greetStrings = new Array[String](3)
greetStrings(0) = "Hello"
greetStrings(1) = ", "
greetStrings(2) = "world!\n"
var i
for (i <- 0 to 2)
print(greetStrings(i))
代碼錯誤:
/Users/tianci/Desktop/helloworld.scala:6: error: '=' expected but ';' found.
for (i <- 0 to 2)
^
one error found
請先看看Scala的教程 – cchantep