Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objectives To perform while (or do-while) loops. To input a number until valid. Prerequisites Do this problem after you have completed the Conditional Expression programs

Objectives

To perform while (or do-while) loops.

To input a number until valid.

Prerequisites

Do this problem after you have completed the Conditional Expression programs and are ready to tackle simple while loops.

Description

Write a program (inRange.scala) that asks the user for two integers, A and B, and then asks the user for an integer within the range of A and B (inclusive). It should continue to ask the user for an integer until it is valid (within the range). If A is greater than B, the two numbers should be swapped (exchanged).

Sample Run

The following are some sample runs. It includes both input and output. For clarity, the input lines are preceded by the symbol > and the shell command (to start the program) is preceded by the symbol $. A sample input and output are separated afterwards.

$ scala inRange.scala Please enter two numbers defining the range. > 3 > 20 Please enter a number between 3 and 20. > 0 That number is not between 3 and 20. Please enter a number between 3 and 20. > -5 That number is not between 3 and 20. Please enter a number between 3 and 20. > 25 That number is not between 3 and 20. Please enter a number between 3 and 20. > 20 The number 20 is between 3 and 20. $ scala inRange.scala Please enter two numbers defining the range. > 45 > 15 Please enter a number between 15 and 45. > 10 That number is not between 15 and 45. Please enter a number between 15 and 45. > 50 That number is not between 15 and 45. Please enter a number between 15 and 45. > 30 The number 30 is between 15 and 45. 

Input

Here is just a sample input to the program.

3 20 0 -5 25 20 

Output

Here is just a sample output to the program. Notice that the prompts are part of the program's output.

Please enter two numbers defining the range. Please enter a number between 3 and 20. That number is not between 3 and 20. Please enter a number between 3 and 20. That number is not between 3 and 20. Please enter a number between 3 and 20. That number is not between 3 and 20. Please enter a number between 3 and 20. The number 20 is between 3 and 20. 

Hint

You will want to use the following Scala language features:

StdIn.readInt to read in integer values

if statement to check various conditions

while (or do-while) to repeat a sequence of statements while a condition is true (such as while the input is invalid).

need in scala

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases A Tour

Authors: Shashi Shekhar, Sanjay Chawla

1st Edition

0130174807, 978-0130174802

More Books

Students also viewed these Databases questions