How to resolve the algorithm Guess the number/With feedback step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Guess the number/With feedback step by step in the Scala 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 Scala programming language
Source code in the scala programming language
import java.util.Random
import java.util.Scanner
val scan = new Scanner(System.in)
val random = new Random
val (from , to) = (1, 100)
val randomNumber = random.nextInt(to - from + 1) + from
var guessedNumber = 0
printf("The number is between %d and %d.\n", from, to)
do {
print("Guess what the number is: ")
guessedNumber = scan.nextInt
if (guessedNumber > randomNumber) println("Your guess is too high!")
else if (guessedNumber < randomNumber) println("Your guess is too low!")
else println("You got it!")
} while (guessedNumber != randomNumber)
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Groovy programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the Action! programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Wren programming language
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the S-lang programming language
You may also check:How to resolve the algorithm Day of the week step by step in the MUMPS programming language