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
答案 0 :(得分:3)
guess_count
是不可变的,(val
),你无法改变它。如果您需要更改变量,请使用var
。
答案 1 :(得分:0)
你可以在scala中增加,但错误的是你正在递增并将值重新分配给最终变量,这就是为什么它会抛出错误,请更改下面的声明然后它会起作用
var guess_count:Int = 0
由于