Advertisement
brilliant_moves

Decimal2Roman.java

Jan 29th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.19 KB | None | 0 0
  1. import java.util.Scanner;   // for user input
  2.  
  3. public class Decimal2Roman {
  4.  
  5.     /**
  6.     *   Program:    Decimal2Roman.java
  7.     *   Purpose:    Convert decimal number to Roman numerals
  8.     *   Creator:    Chris Clarke
  9.     *   Created:    05.11.2012
  10.     */
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.         Scanner scan = new Scanner(System.in);
  15.         int decimal = 0;
  16.  
  17.         if (args.length > 0) {
  18.             try {
  19.                 decimal = Integer.parseInt(args[0]);
  20.             } catch (Exception e) {
  21.                 System.out.println("Not a whole number!");
  22.                 System.exit(1);
  23.             } // end try/catch
  24.         } else {
  25.             System.out.print("Enter a whole number"
  26.              +" for conversion to Roman: ");
  27.             try {
  28.                 decimal = scan.nextInt();
  29.             } catch (Exception e) {
  30.                 System.out.println("That\'s not a whole number!");
  31.                 System.exit(1);
  32.             } // end try/catch
  33.         } // end if/else
  34.  
  35.         System.out.println("In Roman numerals: "
  36.          + getRomanNumerals( decimal));
  37.     } // end main()
  38.  
  39.     public static String getRomanNumerals(int decimal) {
  40.  
  41.         String  ans="";
  42.  
  43.         if (decimal<0) {
  44.             System.out.println("Postive numbers only!");
  45.             return "Error";
  46.         } else if (decimal>=4000) {
  47.             System.out.println("Number too big"
  48.             +" - must be less than 4000");
  49.             return "Error";
  50.         } // end if/else
  51.  
  52.         while (decimal>=1000) {
  53.             ans += "M";
  54.             decimal -= 1000;
  55.         } // end while
  56.  
  57.         if (decimal>=900) {
  58.             ans += "CM";
  59.             decimal -= 900;
  60.         } else if (decimal>=500) {
  61.             ans += "D";
  62.             decimal -= 500;
  63.         } else if (decimal>=400) {
  64.             ans += "CD";
  65.             decimal -= 400;
  66.         } // end if/else
  67.  
  68.         while (decimal>=100) {
  69.             ans += "C";
  70.             decimal -= 100;
  71.         } // end while
  72.  
  73.         if (decimal>=90) {
  74.             ans += "XC";
  75.             decimal -= 90;
  76.         } else if (decimal>=50) {
  77.             ans += "L";
  78.             decimal -= 50;
  79.         } else if (decimal>=40) {
  80.             ans += "XL";
  81.             decimal -= 40;
  82.         } // end if/else
  83.  
  84.         while (decimal>=10) {
  85.             ans += "X";
  86.             decimal -= 10;
  87.         } // end while
  88.  
  89.         if (decimal==9) {
  90.             ans += "IX";
  91.             decimal -= 9;
  92.         } else if (decimal>=5) {
  93.             ans += "V";
  94.             decimal -= 5;
  95.         } else if (decimal==4) {
  96.             ans += "IV";
  97.             decimal -= 4;
  98.         } // end if/else
  99.  
  100.         while (decimal>=1) {
  101.             ans += "I";
  102.             decimal--;
  103.         } // end while
  104.  
  105.         return ans;
  106.  
  107.     } // end getRomanNumerals()
  108. } // end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement