我正在关注的罗马数字到整数转换器:
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,这是不正确的。
请帮助我理解我哪里出错了。
汪汪一只猫
12345678_0001
相关分类