import kotlin.random.Random import kotlin.system.exitProcess fun main() { val randomNum = Random.nextInt(1, 101) println("Welcome to the number guessing game!") println("Guess a number between 1 and 100:") while (true) { print("Enter your guess: ") val input = readLine()?.trim() val guess = input?.toIntOrNull() if (guess == null) { println("Invalid input! Please enter a valid number between 1 and 100.") continue } when { guess < randomNum -> println("Too low! Try again.") guess > randomNum -> println("Too high! Try again.") else -> { println("Congratulations! You guessed the correct number: $randomNum") exitProcess(0) } } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter