Hello!
I only started a couple of days ago to try to learn programming, first with python and now I decided to try Kotlin instead. As an excercise I’m trying to make a simple guessing game just to learn the basics. I’ve watched some Youtube videos and Googled around and tried to piece together bits from here and there.
So far the code below is working as intended, except for the guessCount variable. My intent is to increase it by 1 for every guess the user makes but it seems to be stuck at 0. What am I doing wrong?
import java.util.*
const val rightAnswer = 45
var guessCount = 0
const val maxGuesses = 3
var remainingGuesses = (maxGuesses-guessCount)fun main(){
println(“Welcome to The Guessing Game!”)
while (true){
println(“You have $remainingGuesses guesses left.\nGuess a number:”)
val guessReader = Scanner(System.in
)
var guess = guessReader.nextInt()
guessCount++
println(“You guessed: $guess”)
if(guess != rightAnswer && remainingGuesses > 0){
println(“Guess was wrong.”)
continue}
else if(remainingGuesses < 0){
println(“You’re out of guesses.”)
break}
else {
println(“You guessed the right number, congratulations!”)
break
}}
}