2
 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!") } 

I get this error:

Error:(16, 25) value += is not a member of Int

Expression does not convert to assignment because receiver is not assignable.
guess_count.toInt += 1

2 Answers 2

9

guess_count is immutable, (val), you cannot change it. Use var if you need to change the variable.

Sign up to request clarification or add additional context in comments.

Comments

0

you can increment in scala, but the wrong is you are incrementing and re assigning the value to a final variable, that's why it's throwing error, please change the declaration as below then it will work

var guess_count:Int = 0

Thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.