Guest User

Untitled

a guest
Jan 14th, 2020
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Main;
  2.  
  3. import java.util.HashMap;
  4.  
  5. public class DecimalNumberConverter {
  6.  
  7.     public static void main(String[] args) {
  8.         System.out.println(convert("DCCCXXXIV"));
  9.     }
  10.  
  11.     public static int convert(String value){
  12.         HashMap<Character, Integer> romVal = new HashMap<>();
  13.         romVal.put('M', 1000); romVal.put('D', 500); romVal.put('C', 100); romVal.put('L', 50); romVal.put('X', 10);
  14.         romVal.put('V', 5); romVal.put('I', 1);
  15.         char[] loop = value.toCharArray();
  16.         int result = romVal.get(loop[loop.length-1]);
  17.         for (int i = loop.length-2; i >= 0 ; i--) {
  18.             if(romVal.get(loop[i]) < romVal.get(loop[i+1])){
  19.                 result -= romVal.get(loop[i]);
  20.                 continue;
  21.             }
  22.             result += romVal.get(loop[i]);
  23.         }
  24.  
  25.         return result;
  26.     }
  27.  
  28. }
RAW Paste Data