Answer:
// Scanner class is imported to allow program
// receive input
import java.util.Scanner;
// RomanNumerals class is defined
public class RomanNumerals {
// main method that signify beginning of program execution
public static void main(String args[]) {
// Scanner object scan is created
// it receive input via keyboard
Scanner scan = new Scanner(System.in);
// Prompt is display asking the user to enter number
System.out.println("Enter your number: ");
// the user input is stored at numberOfOrder
int number = scan.nextInt();
// switch statement which takes number as argument
// the switch statement output the correct roman numeral
// depending on user input
switch(number){
case 1:
System.out.println("I");
break;
case 2:
System.out.println("II");
break;
case 3:
System.out.println("III");
break;
case 4:
System.out.println("IV");
break;
case 5:
System.out.println("V");
break;
case 6:
System.out.println("VI");
break;
case 7:
System.out.println("VII");
break;
case 8:
System.out.println("VIII");
break;
case 9:
System.out.println("IX");
break;
case 10:
System.out.println("X");
break;
// this part is executed if user input is not between 1 to 10
default:
System.out.println("Error. Number must be between 1 - 10.");
}
}
}
Explanation:
The program is well commented. A sample image of program output is attached.
The switch statement takes the user input (number) as argument as it goes through each case block in the switch statement and match with the corresponding case to output the roman version of that number. If the number is greater 10 or less than 1; the default block is executed and it display an error message telling the user that number must be between 1 - 10.