|
1 | 1 | package Conversions;
|
2 | 2 |
|
3 | 3 | public class IntegerToRoman {
|
4 |
| - private static int[] allArabianRomanNumbers = new int[] {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; |
5 |
| - private static String[] allRomanNumbers = new String[] {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; |
6 |
| - |
| 4 | + private static int[] allArabianRomanNumbers = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; |
| 5 | + private static String[] allRomanNumbers = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; |
| 6 | + |
7 | 7 | public static String integerToRoman(int num) {
|
8 |
| - if(num <= 0) { |
9 |
| - return ""; |
10 |
| - } |
11 |
| - |
12 |
| - StringBuilder builder = new StringBuilder(); |
13 |
| - |
14 |
| - for(int a = 0;a < allArabianRomanNumbers.length;a++) { |
15 |
| - int times = num / allArabianRomanNumbers[a]; |
16 |
| - for(int b = 0;b < times;b++) { |
17 |
| - builder.append(allRomanNumbers[a]); |
18 |
| - } |
19 |
| - |
20 |
| - num -= times * allArabianRomanNumbers[a]; |
21 |
| - } |
22 |
| - |
23 |
| - return builder.toString(); |
| 8 | + if (num <= 0) { |
| 9 | + return ""; |
| 10 | + } |
| 11 | + |
| 12 | + StringBuilder builder = new StringBuilder(); |
| 13 | + |
| 14 | + for (int a = 0; a < allArabianRomanNumbers.length; a++) { |
| 15 | + int times = num / allArabianRomanNumbers[a]; |
| 16 | + for (int b = 0; b < times; b++) { |
| 17 | + builder.append(allRomanNumbers[a]); |
| 18 | + } |
| 19 | + |
| 20 | + num -= times * allArabianRomanNumbers[a]; |
| 21 | + } |
| 22 | + |
| 23 | + return builder.toString(); |
24 | 24 | }
|
25 |
| - |
| 25 | + |
26 | 26 | public static void main(String[] args) {
|
27 |
| - System.out.println(IntegerToRoman.integerToRoman(2131)); |
28 |
| - } |
| 27 | + System.out.println(IntegerToRoman.integerToRoman(2131)); |
| 28 | + } |
29 | 29 | }
|
0 commit comments