Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Map;
- class RomanConverter {
- static String RomanNumerals(int Int) {
- LinkedHashMap<String, Integer> roman_numerals = new LinkedHashMap<String, Integer>();
- roman_numerals.put("M", 1000);
- roman_numerals.put("CM", 900);
- roman_numerals.put("D", 500);
- roman_numerals.put("CD", 400);
- roman_numerals.put("C", 100);
- roman_numerals.put("XC", 90);
- roman_numerals.put("L", 50);
- roman_numerals.put("XL", 40);
- roman_numerals.put("X", 10);
- roman_numerals.put("IX", 9);
- roman_numerals.put("V", 5);
- roman_numerals.put("IV", 4);
- roman_numerals.put("I", 1);
- StringBuilder res = new StringBuilder();
- for(Map.Entry<String, Integer> entry : roman_numerals.entrySet()){
- int matches = Int/entry.getValue();
- res.append(repeat(entry.getKey(), matches));
- Int = Int % entry.getValue();
- }
- return res.toString();
- }
- private static String repeat(String s, int n) {
- if(s == null) {
- return null;
- }
- return s.repeat(Math.max(0, n));
- }
- }
- public class Main {
- public static void main(String[] args) {
- for(String arg : args)
- {
- int x = Integer.parseInt(arg);
- if (x<=0 || x>=400)
- {
- throw new IllegalArgumentException("liczba " + x + " spoza zakresu 1-3999");
- }
- System.out.println(x + " " + RomanConverter.RomanNumerals(x));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement