我正在使用的罗马数字转整数转换器:
https://www.selftaughtjs.com/algorithm-sundays-converting-roman-numerals/
我尝试将Javascript函数转换为Java:
public class RomanToDecimal {
public static void main (String[] args) {
int result = 0;
int[] decimal = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
// Test string, the number 895
String test = "DCCCXCV";
for (int i = 0; i < decimal.length; i++ ) {
while (test.indexOf(roman[i]) == 0) {
result += decimal[i];
test = test.replace(roman[i], "");
}
}
System.out.println(result);
}
输出结果为615
,这是错误的。
请帮我理解我哪里出了错。