def guessing_game():Unit = {
println("Welcome to the guessing game!!")
val guess_count:Int = 0
val answer = Random.nextInt(50)
var guess_num = scala.io.StdIn.readLine("Input your guess number > ").toInt
while(guess_num != answer || guess_count < 5){
====> guess_count += 1 // <==============================
var situation = if(guess_num > answer){"Your guess is higher!"}else{"Your guess is lower!"}
println(situation)
guess_num = scala.io.StdIn.readLine("Input your guess number > ").toInt
}
if(guess_num == answer){
println("Congratulation....You win!!")
}else{
println("You hav run out of guess!")
}
它说: 错误:(16, 25) value += 不是 Int 的成员 表达式不会转换为赋值,因为接收者不可赋值。 guess_count.toInt += 1
最佳答案
guess_count
是不可变的,(val
),您无法更改它。如果需要更改变量,请使用 var
。
https://stackoverflow.com/questions/46178449/
相关文章:
javascript - 如何在 Gatsby.js 中将时间戳附加到 markdown 前面的内容
apache-spark-sql - pySpark groupby 中的条件聚合
python - Python 中的 str.join() 和 str().join() 有什么区别
amazon-web-services - aws s3 ls 递归 grep 扩展 '.mov'
php - 为什么 Google_Service_Drive 文件的属性只返回 null?
python - 无法将字节连接到 str(转换为 Python3)