number guess

PHOTO EMBED

Thu Nov 21 2024 06:02:46 GMT+0000 (Coordinated Universal Time)

Saved by @coding1

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)
            }
        }
    }
}
content_copyCOPY