Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3.  
  4. class RomanConverter {
  5.     static String RomanNumerals(int Int) {
  6.         LinkedHashMap<String, Integer> roman_numerals = new LinkedHashMap<String, Integer>();
  7.         roman_numerals.put("M", 1000);
  8.         roman_numerals.put("CM", 900);
  9.         roman_numerals.put("D", 500);
  10.         roman_numerals.put("CD", 400);
  11.         roman_numerals.put("C", 100);
  12.         roman_numerals.put("XC", 90);
  13.         roman_numerals.put("L", 50);
  14.         roman_numerals.put("XL", 40);
  15.         roman_numerals.put("X", 10);
  16.         roman_numerals.put("IX", 9);
  17.         roman_numerals.put("V", 5);
  18.         roman_numerals.put("IV", 4);
  19.         roman_numerals.put("I", 1);
  20.         StringBuilder res = new StringBuilder();
  21.         for(Map.Entry<String, Integer> entry : roman_numerals.entrySet()){
  22.             int matches = Int/entry.getValue();
  23.             res.append(repeat(entry.getKey(), matches));
  24.             Int = Int % entry.getValue();
  25.         }
  26.         return res.toString();
  27.     }
  28.     private static String repeat(String s, int n) {
  29.         if(s == null) {
  30.             return null;
  31.         }
  32.         return s.repeat(Math.max(0, n));
  33.     }
  34. }
  35.  
  36. public class Main {
  37.  
  38.     public static void main(String[] args) {
  39.         for(String arg : args)
  40.         {
  41.             int x = Integer.parseInt(arg);
  42.             if (x<=0 || x>=400)
  43.             {
  44.                 throw new IllegalArgumentException("liczba " + x + " spoza zakresu 1-3999");
  45.             }
  46.             System.out.println(x + " " + RomanConverter.RomanNumerals(x));
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement