Sha256: c88c12d7aebc0a9aa41fc89ef24d6e1306d63475662ed3e43065eec09c90ace2
Contents?: true
Size: 1.36 KB
Versions: 396
Compression:
Stored size: 1.36 KB
Contents
import java.util.Map; import java.util.LinkedHashMap; public class RomanNumeral { private int arabicNumeral; private static final Map<Integer, String> arabicToRomanConversions = new LinkedHashMap<>(); static { arabicToRomanConversions.put(1000, "M"); arabicToRomanConversions.put(900, "CM"); arabicToRomanConversions.put(500, "D"); arabicToRomanConversions.put(400, "CD"); arabicToRomanConversions.put(100, "C"); arabicToRomanConversions.put(90, "XC"); arabicToRomanConversions.put(50, "L"); arabicToRomanConversions.put(40, "XL"); arabicToRomanConversions.put(10, "X"); arabicToRomanConversions.put(9, "IX"); arabicToRomanConversions.put(5, "V"); arabicToRomanConversions.put(4, "IV"); arabicToRomanConversions.put(1, "I"); } public RomanNumeral(int arabicNumeral) { this.arabicNumeral = arabicNumeral; } public String getRomanNumeral() { StringBuilder romanNumeral = new StringBuilder(); for (Map.Entry<Integer, String> conversion : arabicToRomanConversions.entrySet()) { while (arabicNumeral / conversion.getKey() > 0) { arabicNumeral -= conversion.getKey(); romanNumeral.append(conversion.getValue()); } } return romanNumeral.toString(); } }
Version data entries
396 entries across 396 versions & 1 rubygems