mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
@ -12,7 +12,7 @@ public class RomanNumeralUtil {
|
||||
|
||||
private static final int MIN_VALUE = 1;
|
||||
private static final int MAX_VALUE = 5999;
|
||||
//1000-5999
|
||||
// 1000-5999
|
||||
private static final String[] RN_M = {
|
||||
"",
|
||||
"M",
|
||||
@ -21,7 +21,7 @@ public class RomanNumeralUtil {
|
||||
"MMMM",
|
||||
"MMMMM",
|
||||
};
|
||||
//100-900
|
||||
// 100-900
|
||||
private static final String[] RN_C = {
|
||||
"",
|
||||
"C",
|
||||
@ -34,7 +34,7 @@ public class RomanNumeralUtil {
|
||||
"DCCC",
|
||||
"CM",
|
||||
};
|
||||
//10-90
|
||||
// 10-90
|
||||
private static final String[] RN_X = {
|
||||
"",
|
||||
"X",
|
||||
@ -47,7 +47,7 @@ public class RomanNumeralUtil {
|
||||
"LXXX",
|
||||
"XC",
|
||||
};
|
||||
//1-9
|
||||
// 1-9
|
||||
private static final String[] RN_I = {
|
||||
"",
|
||||
"I",
|
||||
@ -64,19 +64,10 @@ public class RomanNumeralUtil {
|
||||
public static String generate(int number) {
|
||||
if (number < MIN_VALUE || number > MAX_VALUE) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format(
|
||||
"The number must be in the range [%d, %d]",
|
||||
MIN_VALUE,
|
||||
MAX_VALUE
|
||||
)
|
||||
);
|
||||
String.format("The number must be in the range [%d, %d]", MIN_VALUE, MAX_VALUE));
|
||||
}
|
||||
|
||||
return (
|
||||
RN_M[number / 1000] +
|
||||
RN_C[number % 1000 / 100] +
|
||||
RN_X[number % 100 / 10] +
|
||||
RN_I[number % 10]
|
||||
);
|
||||
return (RN_M[number / 1000] + RN_C[number % 1000 / 100] + RN_X[number % 100 / 10]
|
||||
+ RN_I[number % 10]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user