Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class arabicToRoman {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("Enter a number between 1 and 3999 (-1 to quit): ");
- while (!input.hasNext("-1")) {
- String a = input.next();
- try {
- Integer number = Integer.parseInt(a);
- if ((number <= 3999) && (number > 0)) {
- System.out.println(arabicToRoman(number));
- } else if (number > 3999) {
- System.out.println("Error: number must be between 1 and 3999");
- } else if (number == 0) {
- System.out.println("Error: The Romans did not have a way to represent negative numbers or zero.");
- } else {
- System.out.println("Error: The Romans did not have a way to represent negative numbers or zero.");
- }
- } catch (NumberFormatException e) {
- System.out.println("You did not enter a number!");
- }
- }
- }
- public static String arabicToRoman(int arabic) {
- String a = "";
- while (arabic >= 1000) {
- a += "M";
- arabic -= 1000;
- }
- while (arabic >= 900) {
- a += "CM";
- arabic -= 900;
- }
- while (arabic >= 500) {
- a += "D";
- arabic -= 500;
- }
- while (arabic >= 400) {
- a += "CD";
- arabic -= 400;
- }
- while (arabic >= 100) {
- a += "C";
- arabic -= 100;
- }
- while (arabic >= 90) {
- a += "XC";
- arabic -= 90;
- }
- while (arabic >= 50) {
- a += "L";
- arabic -= 50;
- }
- while (arabic >= 40) {
- a += "XL";
- arabic -= 40;
- }
- while (arabic >= 10) {
- a += "X";
- arabic -= 10;
- }
- while (arabic >= 9) {
- a += "IX";
- arabic -= 10;
- }
- while (arabic >= 5) {
- a += "V";
- arabic -= 5;
- }
- while (arabic >= 4) {
- a += "IV";
- arabic -= 4;
- }
- while (arabic >= 1) {
- a += "I";
- arabic -= 1;
- }
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement