How to resolve the algorithm Guess the number/With feedback step by step in the Kotlin programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Guess the number/With feedback step by step in the Kotlin programming language

Table of Contents

Problem Statement

Write a game (computer program) that follows the following rules:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Guess the number/With feedback step by step in the Kotlin programming language

This code is a simple number guessing game written in Kotlin. The program first chooses a random number between 1 and 20 and then prompts the user to guess the number. The user's guess is then compared to the chosen number and the user is given feedback on whether their guess is correct, higher than the chosen number, or lower than the chosen number. The game continues until the user guesses the correct number.

Here is a more detailed explanation of the code:

  • The first line of the code imports the kotlin.random.Random class, which is used to generate a random number.
  • The second line of the code initializes the n variable to a random number between 1 and 20.
  • The third line of the code prints a message to the user, asking them to guess which number the program has chosen.
  • The fourth line of the code starts a while loop, which will continue until the user guesses the correct number.
  • The fifth line of the code prints a message to the user, asking them to enter their guess.
  • The sixth line of the code reads the user's guess from the console and converts it to an integer.
  • The seventh line of the code uses a when statement to compare the user's guess to the chosen number.
    • If the user's guess is equal to the chosen number, the program prints a message to the user, congratulating them on guessing the correct number, and then returns from the main function.
    • If the user's guess is greater than the chosen number, the program prints a message to the user, telling them that their guess is higher than the chosen number.
    • If the user's guess is less than the chosen number, the program prints a message to the user, telling them that their guess is lower than the chosen number.
    • If the user's guess is not a valid number, the program prints a message to the user, telling them that their guess is inappropriate.
  • The eighth line of the code starts the next iteration of the while loop.

Source code in the kotlin programming language

import kotlin.random.Random

fun main() {
    val n = 1 + rand.nextInt(20)
    println("Guess which number I've chosen in the range 1 to 20\n")
    while (true) {
        print(" Your guess : ")
        val guess = readLine()?.toInt()
        when (guess) {
            n              ->  { println("Correct, well guessed!") ; return }
            in n + 1 .. 20 ->    println("Your guess is higher than the chosen number, try again")
            in 1 .. n - 1  ->    println("Your guess is lower than the chosen number, try again")
            else           ->    println("Your guess is inappropriate, try again")
        }
    }
}


  

You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Unlambda programming language
You may also check:How to resolve the algorithm Fixed length records step by step in the Go programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the Racket programming language
You may also check:How to resolve the algorithm File input/output step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Left factorials step by step in the PicoLisp programming language