112k views
0 votes
Write a program that asks the user to enter a number within the range of 1 through 10. Use a switch statement to display the Roman numeral version of that number. Input Validation: Do not accept a number less than 1 or greater than 10. Prompts And Output Labels. Use the following prompt for input: "Enter a number in the range of 1 - 10: ". The output of the program should be just a Roman numeral, such as VII. CLASS NAMES. Your program class should be called RomanNumerals

1 Answer

2 votes

Answer:

//import the Scanner class to enable user's input

import java.util.Scanner;

// Declare the class header with RomanNumerals as name

public class RomanNumerals {

// Declare the main method where execution begins

public static void main(String[] args) {

//Instantiate an object of the Scanner class to allow user's to enter the number

Scanner userinputscan = new Scanner(System.in);

//Display a message to prompt the user to enter a number

System.out.println("Enter a number in the range of 1 - 10");

//Receive the number from the user and store in an int variable

int usernumber = userinputscan.nextInt();

//Start the switch statement using the usernumber

switch (usernumber) {

//Check if the number is 1

//Display I

//Then break out of the switch statement

case 1 :

System.out.println("I");

break;

//If the number is 2

//Display II

//Then break out of the switch statement

case 2 :

System.out.println("II");

break;

//If the number is 3

//Display III

//Then break

case 3:

System.out.println("III");

break;

//If the number is 4

//Display IV

//Then break

case 4:

System.out.println("IV");

break;

//If the number is 5

//Display V

//Then break

case 5:

System.out.println("V");

break;

//If the number is 6

//Display VI

//Then break

case 6:

System.out.println("VI");

break;

//If the number is 7

//Display VII

//Then break

case 7:

System.out.println("VII");

break;

//If the number is 8

//Display VIII

//Then break

case 8:

System.out.println("VIII");

break;

//If the number is 9

//Display IX

//Then break

case 9:

System.out.println("IX");

break;

//If the number is 10

//Display X

//Then break

case 10:

System.out.println("X");

break;

//If the number is out of range [That is the default case]

//Display an error message.

default:

System.out.println("Error: The number is not within range");

break;

} //End of switch statement

} // End of main method

} // End of class declaration

Step-by-step explanation:

The above code has comments explaining every part of the code. Please go through the comments.

The actual code has been written in bold face to distinguish it from comments.

===============================================================

Sample Output 1:

>> Enter a number in the range of 1 - 10

45

>> Error: The number is not within range

================================================================

Sample Output 2:

>> Enter a number in the range of 1 - 10

8

>> VIII

User Tobias Hoefer
by
4.7k points